kdecore Library API Documentation

kextsock.h

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (C) 2000-2004 Thiago Macieira <thiago.macieira@kdemail.net>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this library; see the file COPYING.LIB.  If not, write to
00017  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  *  Boston, MA 02111-1307, USA.
00019  */
00020 #ifndef KEXTSOCK_H
00021 #define KEXTSOCK_H
00022 
00023 #include <sys/time.h>
00024 
00025 #include <qstring.h>
00026 #include <qptrlist.h>
00027 #include <qiodevice.h>
00028 
00029 #include "kbufferedio.h"
00030 #include "ksockaddr.h"
00031 #include "kdemacros.h"
00032 
00033 /* External reference to netdb.h */
00034 struct addrinfo;
00035 struct kde_addrinfo;
00036 class KAddressInfo;     /* our abstraction of it */
00037 class QSocketNotifier;
00038 
00039 /*
00040  * This is extending QIODevice's error codes
00041  *
00042  * According to qiodevice.h, the last error is IO_UnspecifiedError
00043  * These errors will never occur in functions declared in QIODevice
00044  * (except open, but you shouldn't call open)
00045  */
00046 #define IO_ListenError      (IO_UnspecifiedError+1)
00047 #define IO_AcceptError      (IO_UnspecifiedError+2)
00048 #define IO_LookupError      (IO_UnspecifiedError+3)
00049 
00050 class KExtendedSocketPrivate;
00088 class KExtendedSocket: public KBufferedIO // public QObject, public QIODevice
00089 {
00090   Q_OBJECT
00091 
00092 public:
00096   enum Flags
00097   {
00098     /* socket address families */
00099     /*
00100      * NOTE: if you change this, you have to change function valid_socket() as well
00101      * These values are hard coded!
00102      */
00103     anySocket = 0x00,
00104     knownSocket = 0x01,
00105     unixSocket = knownSocket | 0x02,
00106     inetSocket = knownSocket | 0x04,
00107     ipv4Socket = inetSocket | 0x100,
00108     ipv6Socket = inetSocket | 0x200,
00109 
00110     passiveSocket = 0x1000, /* passive socket (i.e., one that accepts connections) */
00111     canonName = 0x2000,     /* request that the canon name be found */
00112     noResolve = 0x4000,     /* do not attempt to resolve, treat as numeric host */
00113 
00114     streamSocket = 0x8000,  /* request a streaming socket (e.g., TCP) */
00115     datagramSocket = 0x10000,   /* request a datagram socket (e.g., UDP) */
00116     rawSocket = 0x20000,    /* request a raw socket. This probably requires privileges */
00117 
00118     inputBufferedSocket = 0x200000, /* buffer input in this socket */
00119     outputBufferedSocket = 0x400000, /* buffer output in this socket */
00120     bufferedSocket = 0x600000   /* make this a fully buffered socket */
00121   };
00122 
00128   enum SockStatus
00129   {
00130     // the numbers are scattered so that we leave room for future expansion
00131     error = -1,         // invalid status!
00132 
00133     nothing = 0,        // no status, the class has just been created
00134 
00135     lookupInProgress = 50,  // lookup is in progress. Signals will be sent
00136     lookupDone = 70,        // lookup has been done. Flags cannot be changed
00137                 // from this point on
00138 
00139     created = 100,      // ::socket() has been called, a socket exists
00140     bound = 140,        // socket has been bound
00141 
00142     connecting = 200,       // socket is connecting (not passiveSocket)
00143     connected = 220,        // socket has connected (not passiveSocket)
00144 
00145     listening = 200,        // socket is listening (passiveSocket)
00146     accepting = 220,        // socket is accepting (passiveSocket)
00147 
00148     closing = 350,      // socket is closing (delayed close)
00149 
00150     done = 400          // socket has been closed
00151   };
00152 
00153 public:
00157   KExtendedSocket();
00158 
00175   KExtendedSocket(const QString& host, int port, int flags = 0);
00176 
00193   KExtendedSocket(const QString& host, const QString& service, int flags = 0);
00194 
00199   virtual ~KExtendedSocket();
00200 
00206   void reset();
00207 
00208   /*
00209    * --- status, flags and internal variables --- *
00210    */
00211 
00217   int socketStatus() const;
00218 
00225   int systemError() const;
00226 
00232   int setSocketFlags(int flags);
00233 
00239   int socketFlags() const;
00240 
00254   bool setHost(const QString& host);
00255 
00260   QString host() const;
00261 
00266   bool setPort(int port);
00267 
00277   bool setPort(const QString& port);
00278 
00283   QString port() const;
00284 
00294   bool setAddress(const QString& host, int port);
00295 
00305   bool setAddress(const QString& host, const QString& serv);
00306 
00312   bool setBindHost(const QString& host);
00313 
00318   bool unsetBindHost();
00319 
00324   QString bindHost() const;
00325 
00331   bool setBindPort(int port);
00332 
00338   bool setBindPort(const QString& service);
00339 
00344   bool unsetBindPort();
00345 
00350   QString bindPort() const;
00351 
00359   bool setBindAddress(const QString& host, int port);
00360 
00368   bool setBindAddress(const QString& host, const QString& service);
00369 
00375   bool unsetBindAddress();
00376 
00388   bool setTimeout(int secs, int usecs = 0);
00389 
00394   timeval timeout() const;
00395 
00404   bool setBlockingMode(bool enable);
00405 
00410   bool blockingMode();
00411 
00421   bool setAddressReusable(bool enable);
00422 
00427   bool addressReusable();
00428 
00447   bool setIPv6Only(bool enable);
00448 
00455   bool isIPv6Only();
00456 
00474   virtual bool setBufferSize(int rsize, int wsize = -2);
00475 
00481   const ::KSocketAddress *localAddress();
00482 
00489   const ::KSocketAddress *peerAddress();
00490 
00495   inline int fd() const
00496   { return sockfd; }
00497 
00498   /*
00499    * -- socket creation -- *
00500    */
00501 
00509   virtual int lookup();
00510 
00529   virtual int startAsyncLookup();
00530 
00534   virtual void cancelAsyncLookup();
00535 
00543   virtual int listen(int N = 5); // 5 is arbitrary
00544 
00559   virtual int accept(KExtendedSocket *&sock);
00560 
00584   virtual int connect();
00585 
00600   virtual int startAsyncConnect();
00601 
00605   virtual void cancelAsyncConnect();
00606 
00617   virtual bool open(int mode = IO_Raw | IO_ReadWrite);
00618 
00626   virtual void close();
00627 
00633   virtual void closeNow();
00634 
00648   virtual void release();
00649 
00650   /*
00651    * -- I/O --
00652    */
00653 
00669   virtual void flush();
00670 
00675   virtual inline Q_ULONG size() const
00676   { return 0; }
00677 
00682   virtual inline Q_ULONG at() const
00683   { return 0; }
00684 
00690   virtual inline bool at(int i)
00691   { Q_UNUSED(i);return true; }
00692 
00698   virtual inline bool atEnd() const
00699   { return false; }
00700 
00730   virtual Q_LONG readBlock(char *data, Q_ULONG maxlen);
00731 
00755   virtual Q_LONG writeBlock(const char *data, Q_ULONG len);
00756 
00771   virtual int peekBlock(char *data, uint maxlen);
00772 
00779   virtual int unreadBlock(const char *data, uint len);
00780 
00790   virtual int bytesAvailable() const;
00791 
00801   virtual int waitForMore(int msec);
00802 
00807   virtual int getch();
00808 
00814   virtual int putch(int ch);
00815 
00820   virtual int ungetch(int)
00821   { return -1; }
00822 
00833   virtual void enableRead(bool enable);
00834 
00844   virtual void enableWrite(bool enable);
00845 
00846 signals:
00852   void lookupFinished(int count);
00853 
00857   void connectionSuccess();
00858 
00864   void connectionFailed(int error);
00865 
00871   void readyAccept();
00872 
00873 protected:
00874   int sockfd;           // file descriptor of the socket
00875 
00876 protected slots:
00877 
00878   void socketActivityRead();
00879   void socketActivityWrite();
00880   void dnsResultsReady();
00881   void startAsyncConnectSlot();
00882   void connectionEvent();
00883 
00884 protected:
00885 
00886   QSocketNotifier *readNotifier();
00887   QSocketNotifier *writeNotifier();
00888 
00889 private:
00890 
00891   // protection against accidental use
00892   KExtendedSocket(KExtendedSocket&);
00893   KExtendedSocket& operator=(KExtendedSocket&);
00894 
00899   static int doLookup(const QString& host, const QString& serv, addrinfo& hint,
00900               kde_addrinfo** result);
00901 
00902 protected:
00906   void setError(int errorkind, int error);
00907 
00908   inline void cleanError()
00909   { setError(IO_Ok, 0); }
00910 
00914   void setSocketStatus(int status);
00915 
00916 public:
00930   static int resolve(sockaddr* sock, ksocklen_t len, QString& host, QString& port, int flags = 0) KDE_DEPRECATED;
00931 
00944   static int resolve(::KSocketAddress* sock, QString& host, QString& port, int flags = 0) KDE_DEPRECATED;
00945 
00966   static QPtrList<KAddressInfo> lookup(const QString& host, const QString& port, int flags = 0, int *error = 0) KDE_DEPRECATED;
00967 
00974   static ::KSocketAddress *localAddress(int fd) KDE_DEPRECATED;
00975 
00983   static ::KSocketAddress *peerAddress(int fd) KDE_DEPRECATED;
00984 
00991   static QString strError(int code, int syserr);
00992 
01002   static bool setAddressReusable(int fd, bool enable) KDE_DEPRECATED;
01003 
01004 protected:
01005   virtual void virtual_hook( int id, void* data );
01006 private:
01007   KExtendedSocketPrivate *d;
01008 
01009   friend class KSocket;
01010   friend class KServerSocket;
01011 };
01012 
01019 class KDE_DEPRECATED KAddressInfo
01020 {
01021 private:
01022   addrinfo *ai;
01023   ::KSocketAddress *addr;
01024 
01025   inline KAddressInfo() : ai(0), addr(0)
01026   { }
01027 
01028   //  KAddressInfo(addrinfo *ai);
01029   KAddressInfo(KAddressInfo&) { }
01030   KAddressInfo& operator=(KAddressInfo&) { return *this; }
01031 
01032 public:
01033   ~KAddressInfo();
01034 
01039   inline operator const ::KSocketAddress*() const
01040   { return addr; }
01041 
01045   inline operator const addrinfo&() const
01046   { return *ai; }
01047 
01052   inline operator const addrinfo*() const
01053   { return ai; }
01054 
01060   inline const ::KSocketAddress* address() const
01061   { return addr; }
01062 
01067   int flags() const;
01068 
01073   int family() const;
01074 
01079   int socktype() const;
01080 
01085   int protocol() const;
01086 
01087 
01093   const char* canonname() const;
01094 
01099   inline int length() const
01100   { if (addr) return addr->size(); return 0; }
01101 
01102   friend class KExtendedSocket;
01103 };
01104 
01105 #endif // KEXTSOCK_H
KDE Logo
This file is part of the documentation for kdecore Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Jan 15 13:32:28 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003