File indexing completed on 2023-10-03 03:16:16
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 KSERVERSOCKET_H 0026 #define KSERVERSOCKET_H 0027 0028 #include <QObject> 0029 #include "k3socketbase.h" 0030 #include "k3streamsocket.h" 0031 0032 namespace KNetwork 0033 { 0034 0035 class KStreamSocket; 0036 class KResolver; 0037 class KResolverResults; 0038 0039 class KServerSocketPrivate; 0040 /** 0041 * @class KServerSocket k3serversocket.h k3serversocket.h 0042 * @brief A server socket for accepting connections. 0043 * 0044 * This class provides functionality for creating a socket to 0045 * listen for incoming connections and subsequently accept them. 0046 * 0047 * To use this class, you must first set the parameters for the listening 0048 * socket's address, then place it in listening mode. 0049 * 0050 * A typical example would look like: 0051 * \code 0052 * QString service = "http"; 0053 * KServerSocket *ss = new KServerSocket(service); 0054 * connect(ss, SIGNAL(readyAccept()), this, SLOT(slotReadyAccept())); 0055 * connect(ss, SIGNAL(gotError(int)), this, SLOT(slotSocketError(int))); 0056 * ss->listen(); 0057 * \endcode 0058 * 0059 * In this case, this class will place the socket into listening mode on the 0060 * service pointed to by @p service and will emit the readyAccept() signal 0061 * when a connection is ready for accepting. The called slot is responsible for 0062 * calling accept(). 0063 * 0064 * The location of the services file (where @p service is looked up) 0065 * is defined by _PATH_SERVICES in /usr/include/netdb.h. This is 0066 * usually set to /etc/services. 0067 * See RFC 1700 for more information on services. 0068 * You can specify @p service as a port number directly, rather than as a service 0069 * name. This is discouraged as it prevents the end user from easily modifying 0070 * the port number. 0071 * 0072 * For another example of usage, this below code attempts to make a connection on any port within a range: 0073 * \code 0074 * KServerSocket *ss = new KServerSocket(); 0075 * ss->setFamily(KResolver::InetFamily); 0076 * bool found = false; 0077 * for( unsigned int port = firstport; port <= lastport; ++port) { 0078 * ss->setAddress( QString::number( port ) ); 0079 * bool success = ss->listen(); 0080 * if( found = ( success && ss->error() == 0081 * KSocketBase::NoError ) ) 0082 * break; 0083 * ss->close(); 0084 * } 0085 * if( !found ) { 0086 * // Couldn't connect to any port. 0087 * } else { 0088 * connect(ss, SIGNAL(readyAccept()), this, SLOT(slotReadyAccept())); 0089 * connect(ss, SIGNAL(gotError(int)), this, SLOT(slotSocketError(int))); 0090 * ss->listen(); 0091 * } 0092 * \endcode 0093 * 0094 * The called slot slotReadyAccept() is responsible for calling 0095 * accept(). 0096 * 0097 * It is important to note that accept() can return either an 0098 * object of type KNetwork::KStreamSocket or 0099 * KNetwork::KBufferedSocket (default). If you want to accept a 0100 * non-buffered socket, you must first call setAcceptBuffered. 0101 * 0102 * @warning If you use KServerSocket in an auxiliary (non-GUI) thread, 0103 * you need to accept only KNetwork::KStreamSocket objects. 0104 * 0105 * @see KNetwork::KStreamSocket, KNetwork::KBufferedSocket 0106 * @author Thiago Macieira <thiago@kde.org> 0107 * @deprecated Use KSocketFactory or KLocalSocket instead 0108 */ 0109 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KServerSocket: public QObject, public KPassiveSocketBase 0110 { 0111 Q_OBJECT 0112 public: 0113 /** 0114 * Default constructor. 0115 * 0116 * If the binding address isn't changed by setAddress, this socket will 0117 * bind to all interfaces on this node and the port will be selected by the 0118 * operating system. 0119 * 0120 * @param parent the parent QObject object 0121 */ 0122 KServerSocket(QObject *parent = nullptr); 0123 0124 /** 0125 * Construct this object specifying the service to listen on. 0126 * 0127 * If the binding address isn't changed by setAddress, this socket will 0128 * bind to all interfaces and will listen on the port specified by 0129 * @p service. This is either a service name (e.g. 'www') or a port 0130 * number (e.g. '80'). 0131 * 0132 * The location of the services file (where @p service is looked up) 0133 * is defined by _PATH_SERVICES in /usr/include/netdb.h. This is 0134 * usually set to /etc/services. 0135 * See RFC 1700 for more information on services. 0136 * 0137 * @param service the service name to listen on 0138 * @param parent the parent QObject object 0139 */ 0140 KDELIBS4SUPPORT_DEPRECATED explicit KServerSocket(const QString &service, QObject *parent = nullptr); 0141 0142 /** 0143 * Construct this object specifying the node and service names to listen on. 0144 * 0145 * If the binding address isn't changed by setAddress, this socket will 0146 * bind to the interface specified by @p node and the port specified by 0147 * @p service. This is either a service name (e.g. 'www') or a port 0148 * number (e.g. '80'). 0149 * 0150 * The location of the services file (where @p service is looked up) 0151 * is defined by _PATH_SERVICES in /usr/include/netdb.h. This is 0152 * usually set to /etc/services. 0153 * See RFC 1700 for more information on services. 0154 * 0155 * @param node the node to bind to 0156 * @param service the service port to listen on 0157 * @param parent the parent QObject object 0158 */ 0159 KServerSocket(const QString &node, const QString &service, 0160 QObject *parent = nullptr); 0161 0162 /** 0163 * Destructor. This will close the socket, if open. 0164 * 0165 * Note, however, that accepted sockets do not get closed when this 0166 * object closes. 0167 */ 0168 ~KServerSocket() override; 0169 0170 protected: 0171 /** 0172 * Sets the socket options. Reimplemented from KSocketBase. 0173 */ 0174 bool setSocketOptions(int opts) override; 0175 0176 public: 0177 /** 0178 * Returns the internal KResolver object used for 0179 * looking up the host name and service. 0180 * 0181 * This can be used to set extra options to the 0182 * lookup process other than the default values, as well 0183 * as obtaining the error codes in case of lookup failure. 0184 */ 0185 KResolver &resolver() const; 0186 0187 /** 0188 * Returns the internal list of resolved results for the binding address. 0189 */ 0190 const KResolverResults &resolverResults() const; 0191 0192 /** 0193 * Enables or disables name resolution. If this flag is set to true, 0194 * the bind() operation will trigger name lookup 0195 * operations (i.e., converting a hostname into its binary form). 0196 * If the flag is set to false, those operations will instead 0197 * try to convert a string representation of an address without 0198 * attempting name resolution. 0199 * 0200 * This is useful, for instance, when IP addresses are in 0201 * their string representation (such as "1.2.3.4") or come 0202 * from other sources like KSocketAddress. 0203 * 0204 * @param enable whether to enable 0205 */ 0206 void setResolutionEnabled(bool enable); 0207 0208 /** 0209 * Sets the allowed families for the resolutions. 0210 * 0211 * @param families the families that we want/accept 0212 * @see KResolver::SocketFamilies for possible values 0213 */ 0214 void setFamily(int families); 0215 0216 /** 0217 * Sets the address on which we will listen. The port to listen on is given by 0218 * @p service, and we will bind to all interfaces. To let the operating system choose a 0219 * port, set the service to "0". @p service can either be a service name 0220 * (e.g. 'www') or a port number (e.g. '80'). 0221 * 0222 * The location of the services file (where @p service is looked up) 0223 * is defined by _PATH_SERVICES in /usr/include/netdb.h. This is 0224 * usually set to /etc/services. 0225 * See RFC 1700 for more information on services. 0226 * 0227 * @param service the service name to listen on 0228 */ 0229 void setAddress(const QString &service); 0230 0231 /** 0232 * @overload 0233 * Sets the address on which we will listen. This will cause the socket to listen 0234 * only on the interface given by @p node and on the port given by @p service. 0235 * @p service can either be a service name (e.g. 'www') or a port number 0236 * (e.g. '80'). 0237 * 0238 * The location of the services file (where @p service is looked up) 0239 * is defined by _PATH_SERVICES in /usr/include/netdb.h. This is 0240 * usually set to /etc/services. 0241 * See RFC 1700 for more information on services. 0242 * 0243 * @param node the node to bind to 0244 * @param service the service port to listen on 0245 */ 0246 void setAddress(const QString &node, const QString &service); 0247 0248 /** 0249 * Sets the timeout for accepting. When you call accept(), 0250 * it will wait at most @p msecs milliseconds or return with an error 0251 * (returning a NULL object). 0252 * 0253 * @param msecs the time in milliseconds to wait, 0 to wait forever 0254 */ 0255 void setTimeout(int msecs); 0256 0257 /** 0258 * Starts the lookup for peer and local hostnames as 0259 * well as their services. 0260 * 0261 * If the blocking mode for this object is on, this function will 0262 * wait for the lookup results to be available (by calling the 0263 * KResolver::wait() method on the resolver objects). 0264 * 0265 * When the lookup is done, the signal hostFound() will be 0266 * emitted (only once, even if we're doing a double lookup). 0267 * If the lookup failed (for any of the two lookups) the 0268 * gotError() signal will be emitted with the appropriate 0269 * error condition (see KSocketBase::SocketError). 0270 * 0271 * This function returns true on success and false on error. Note that 0272 * this is not the lookup result! 0273 */ 0274 virtual bool lookup(); 0275 0276 /** 0277 * Binds this socket to the given nodename and service, 0278 * or use the default ones if none are given. 0279 * 0280 * Upon successful binding, the bound() signal will be 0281 * emitted. If an error is found, the gotError() 0282 * signal will be emitted. 0283 * 0284 * This function returns true on success. 0285 * 0286 * @param node the nodename 0287 * @param service the service 0288 */ 0289 virtual bool bind(const QString &node, const QString &service); 0290 0291 /** 0292 * Binds the socket to the given service name. 0293 * @overload 0294 * 0295 * @param service the service 0296 */ 0297 virtual bool bind(const QString &service); 0298 0299 /** 0300 * Binds the socket to the addresses previously set with setAddress(). 0301 * @overload 0302 * 0303 */ 0304 virtual bool bind(); 0305 0306 /** 0307 * Connect this socket to this specific address. Reimplemented from KSocketBase. 0308 * 0309 * Unlike bind(const QString&, const QString&) above, this function 0310 * really does bind the socket. No lookup is performed. The bound() signal 0311 * will be emitted. 0312 */ 0313 bool bind(const KResolverEntry &address) override; 0314 0315 /** 0316 * Puts this socket into listening mode. Reimplemented from KPassiveSocketBase. 0317 * 0318 * Placing a socket into listening mode means it will be able to receive incoming 0319 * connections through the accept() method. 0320 * 0321 * If you do not call this method but call accept() directly, the socket will 0322 * be placed into listening mode automatically. 0323 * 0324 * @param backlog the number of connection the system is to 0325 * queue without accept() being called 0326 * @returns true if the socket is now in listening mode. 0327 */ 0328 bool listen(int backlog = 5) override; // 5 is arbitrary 0329 0330 /** 0331 * Closes this socket. 0332 */ 0333 void close() override; 0334 0335 /** 0336 * Toggles whether the accepted socket will be buffered or not. 0337 * That is, the accept() function will always return a KStreamSocket 0338 * object or descended from it. If buffering is enabled, the class 0339 * to be returned will be KBufferedSocket. 0340 * 0341 * By default, this flag is set to true. 0342 * 0343 * @param enable whether to set the accepted socket to 0344 * buffered mode 0345 */ 0346 void setAcceptBuffered(bool enable); 0347 0348 /** 0349 * Accepts one incoming connection and return the associated, open 0350 * socket. 0351 * 0352 * If this function cannot accept a new connection, it will return NULL. 0353 * The specific object class returned by this function may vary according 0354 * to the implementation: derived classes may return specialized objects 0355 * descended from KStreamSocket. 0356 * 0357 * @sa KBufferedSocket 0358 * @sa setAcceptBuffered 0359 */ 0360 KStreamSocket *accept() override; 0361 0362 /** 0363 * Returns this socket's local address. 0364 */ 0365 KSocketAddress localAddress() const override; 0366 0367 /** 0368 * Returns this socket's externally-visible address if know. 0369 */ 0370 KSocketAddress externalAddress() const override; 0371 0372 private Q_SLOTS: 0373 void lookupFinishedSlot(); 0374 0375 Q_SIGNALS: 0376 /** 0377 * This signal is emitted when this object finds an error. 0378 * The @p code parameter contains the error code that can 0379 * also be found by calling error(). 0380 */ 0381 void gotError(int code); 0382 0383 /** 0384 * This signal is emitted when the lookup is successfully completed. 0385 */ 0386 void hostFound(); 0387 0388 /** 0389 * This signal is emitted when the socket successfully binds 0390 * to an address. 0391 * 0392 * @param local the local address we bound to 0393 */ 0394 void bound(const KNetwork::KResolverEntry &local); 0395 0396 /** 0397 * This signal is emitted when the socket completes the 0398 * closing/shut down process. 0399 */ 0400 void closed(); 0401 0402 /** 0403 * This signal is emitted whenever the socket is ready for 0404 * accepting -- i.e., there is at least one connection waiting to 0405 * be accepted. 0406 */ 0407 void readyAccept(); 0408 0409 protected: 0410 /** 0411 * Convenience function to set this object's error code to match 0412 * that of the socket device. 0413 */ 0414 void copyError(); 0415 0416 private: 0417 bool doBind(); 0418 bool doListen(); 0419 0420 private: 0421 KServerSocket(const KServerSocket &); 0422 KServerSocket &operator=(const KServerSocket &); 0423 0424 KServerSocketPrivate *const d; 0425 }; 0426 0427 } // namespace KNetwork 0428 0429 #endif