00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00034 struct addrinfo;
00035 struct kde_addrinfo;
00036 class KAddressInfo;
00037 class QSocketNotifier;
00038
00039
00040
00041
00042
00043
00044
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
00089 {
00090 Q_OBJECT
00091
00092 public:
00096 enum Flags
00097 {
00098
00099
00100
00101
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,
00111 canonName = 0x2000,
00112 noResolve = 0x4000,
00113
00114 streamSocket = 0x8000,
00115 datagramSocket = 0x10000,
00116 rawSocket = 0x20000,
00117
00118 inputBufferedSocket = 0x200000,
00119 outputBufferedSocket = 0x400000,
00120 bufferedSocket = 0x600000
00121 };
00122
00128 enum SockStatus
00129 {
00130
00131 error = -1,
00132
00133 nothing = 0,
00134
00135 lookupInProgress = 50,
00136 lookupDone = 70,
00137
00138
00139 created = 100,
00140 bound = 140,
00141
00142 connecting = 200,
00143 connected = 220,
00144
00145 listening = 200,
00146 accepting = 220,
00147
00148 closing = 350,
00149
00150 done = 400
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
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
00500
00501
00509 virtual int lookup();
00510
00529 virtual int startAsyncLookup();
00530
00534 virtual void cancelAsyncLookup();
00535
00543 virtual int listen(int N = 5);
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
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;
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
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
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