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
00032
00033 struct addrinfo;
00034 struct kde_addrinfo;
00035 class KAddressInfo;
00036 class QSocketNotifier;
00037
00038
00039
00040
00041
00042
00043
00044
00045 #define IO_ListenError (IO_UnspecifiedError+1)
00046 #define IO_AcceptError (IO_UnspecifiedError+2)
00047 #define IO_LookupError (IO_UnspecifiedError+3)
00048
00049 class KExtendedSocketPrivate;
00083 class KExtendedSocket: public KBufferedIO
00084 {
00085 Q_OBJECT
00086
00087 public:
00091 enum Flags
00092 {
00093
00094
00095
00096
00097
00098 anySocket = 0x00,
00099 knownSocket = 0x01,
00100 unixSocket = knownSocket | 0x02,
00101 inetSocket = knownSocket | 0x04,
00102 ipv4Socket = inetSocket | 0x100,
00103 ipv6Socket = inetSocket | 0x200,
00104
00105 passiveSocket = 0x1000,
00106 canonName = 0x2000,
00107 noResolve = 0x4000,
00108
00109 streamSocket = 0x8000,
00110 datagramSocket = 0x10000,
00111 rawSocket = 0x20000,
00112
00113 inputBufferedSocket = 0x200000,
00114 outputBufferedSocket = 0x400000,
00115 bufferedSocket = 0x600000
00116 };
00117
00123 enum SockStatus
00124 {
00125
00126 error = -1,
00127
00128 nothing = 0,
00129
00130 lookupInProgress = 50,
00131 lookupDone = 70,
00132
00133
00134 created = 100,
00135 bound = 140,
00136
00137 connecting = 200,
00138 connected = 220,
00139
00140 listening = 200,
00141 accepting = 220,
00142
00143 closing = 350,
00144
00145 done = 400
00146 };
00147
00148 public:
00152 KExtendedSocket();
00153
00160 KExtendedSocket(const QString& host, int port, int flags = 0);
00161
00168 KExtendedSocket(const QString& host, const QString& service, int flags = 0);
00169
00174 virtual ~KExtendedSocket();
00175
00181 void reset();
00182
00183
00184
00185
00186
00192 int socketStatus() const;
00193
00200 int systemError() const;
00201
00207 int setSocketFlags(int flags);
00208
00214 int socketFlags() const;
00215
00221 bool setHost(const QString& host);
00222
00227 QString host() const;
00228
00233 bool setPort(int port);
00234
00240 bool setPort(const QString& service);
00241
00246 QString port() const;
00247
00254 bool setAddress(const QString& host, int port);
00255
00262 bool setAddress(const QString& host, const QString& serv);
00263
00269 bool setBindHost(const QString& host);
00270
00275 bool unsetBindHost();
00276
00281 QString bindHost() const;
00282
00288 bool setBindPort(int port);
00289
00295 bool setBindPort(const QString& service);
00296
00301 bool unsetBindPort();
00302
00307 QString bindPort() const;
00308
00316 bool setBindAddress(const QString& host, int port);
00317
00325 bool setBindAddress(const QString& host, const QString& service);
00326
00332 bool unsetBindAddress();
00333
00345 bool setTimeout(int secs, int usecs = 0);
00346
00351 timeval timeout() const;
00352
00361 bool setBlockingMode(bool enable);
00362
00367 bool blockingMode();
00368
00378 bool setAddressReusable(bool enable);
00379
00384 bool addressReusable();
00385
00407 virtual bool setBufferSize(int rsize, int wsize = -2);
00408
00414 const KSocketAddress *localAddress();
00415
00422 const KSocketAddress *peerAddress();
00423
00428 inline int fd() const
00429 { return sockfd; }
00430
00431
00432
00433
00434
00442 virtual int lookup();
00443
00462 virtual int startAsyncLookup();
00463
00467 virtual void cancelAsyncLookup();
00468
00476 virtual int listen(int N = 5);
00477
00492 virtual int accept(KExtendedSocket *&sock);
00493
00517 virtual int connect();
00518
00529 virtual int startAsyncConnect();
00530
00534 virtual void cancelAsyncConnect();
00535
00546 virtual bool open(int mode = IO_Raw | IO_ReadWrite);
00547
00555 virtual void close();
00556
00562 virtual void closeNow();
00563
00577 virtual void release();
00578
00579
00580
00581
00582
00598 virtual void flush();
00599
00604 virtual inline Q_ULONG size() const
00605 { return 0; }
00606
00611 virtual inline Q_ULONG at() const
00612 { return 0; }
00613
00619 virtual inline bool at(int i)
00620 { Q_UNUSED(i);return true; }
00621
00627 virtual inline bool atEnd() const
00628 { return false; }
00629
00659 virtual Q_LONG readBlock(char *data, Q_ULONG maxlen);
00660
00684 virtual Q_LONG writeBlock(const char *data, Q_ULONG len);
00685
00700 virtual int peekBlock(char *data, uint maxlen);
00701
00708 virtual int unreadBlock(const char *data, uint len);
00709
00719 virtual int bytesAvailable() const;
00720
00730 virtual int waitForMore(int msec);
00731
00736 virtual int getch();
00737
00743 virtual int putch(int ch);
00744
00749 virtual int ungetch(int)
00750 { return -1; }
00751
00762 virtual void enableRead(bool enable);
00763
00773 virtual void enableWrite(bool enable);
00774
00775 signals:
00781 void lookupFinished(int count);
00782
00786 void connectionSuccess();
00787
00793 void connectionFailed(int error);
00794
00800 void readyAccept();
00801
00802 protected:
00803 int sockfd;
00804
00805 protected slots:
00806
00807 void socketActivityRead();
00808 void socketActivityWrite();
00809 void dnsResultsReady();
00810 void startAsyncConnectSlot();
00811 void connectionEvent();
00812
00813 protected:
00814
00815 QSocketNotifier *readNotifier();
00816 QSocketNotifier *writeNotifier();
00817
00818 private:
00819
00820
00821 KExtendedSocket(KExtendedSocket&);
00822 KExtendedSocket& operator=(KExtendedSocket&);
00823
00828 static int doLookup(const QString& host, const QString& serv, addrinfo& hint,
00829 kde_addrinfo** result);
00830
00831 protected:
00835 void setError(int errorkind, int error);
00836
00837 inline void cleanError()
00838 { setError(IO_Ok, 0); }
00839
00843 void setSocketStatus(int status);
00844
00845 public:
00858 static int resolve(sockaddr* sock, ksocklen_t len, QString& host, QString& port, int flags = 0);
00859
00872 static int resolve(KSocketAddress* sock, QString& host, QString& port, int flags = 0);
00873
00892 static QPtrList<KAddressInfo> lookup(const QString& host, const QString& port, int flags = 0, int *error = 0);
00893
00900 static KSocketAddress *localAddress(int fd);
00901
00909 static KSocketAddress *peerAddress(int fd);
00910
00917 static QString strError(int code, int syserr);
00918
00928 static bool setAddressReusable(int fd, bool enable);
00929
00930 protected:
00931 virtual void virtual_hook( int id, void* data );
00932 private:
00933 KExtendedSocketPrivate *d;
00934
00935 friend class KSocket;
00936 friend class KServerSocket;
00937 };
00938
00943 class KAddressInfo
00944 {
00945 private:
00946 addrinfo *ai;
00947 KSocketAddress *addr;
00948
00949 inline KAddressInfo() : ai(0), addr(0)
00950 { }
00951
00952 KAddressInfo(addrinfo *ai);
00953 KAddressInfo(KAddressInfo&) { }
00954 KAddressInfo& operator=(KAddressInfo&) { return *this; }
00955
00956 public:
00957 ~KAddressInfo();
00958
00963 inline operator const KSocketAddress*() const
00964 { return addr; }
00965
00969 inline operator const addrinfo&() const
00970 { return *ai; }
00971
00976 inline operator const addrinfo*() const
00977 { return ai; }
00978
00984 inline const KSocketAddress* address() const
00985 { return addr; }
00986
00991 int flags() const;
00992
00997 int family() const;
00998
01003 int socktype() const;
01004
01009 int protocol() const;
01010
01011
01017 const char* canonname() const;
01018
01023 inline int length() const
01024 { if (addr) return addr->size(); return 0; }
01025
01026 friend class KExtendedSocket;
01027 };
01028
01029 #endif // KEXTSOCK_H