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., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 #ifndef KEXTSOCK_H 00021 #define KEXTSOCK_H 00022 00023 #include "kdelibs_export.h" 00024 00025 #ifdef Q_OS_UNIX 00026 00027 #include <sys/time.h> 00028 00029 #include <qstring.h> 00030 #include <qptrlist.h> 00031 #include <qiodevice.h> 00032 00033 #include "kbufferedio.h" 00034 #include "ksockaddr.h" 00035 00036 /* External reference to netdb.h */ 00037 struct addrinfo; 00038 struct kde_addrinfo; 00039 class KAddressInfo; /* our abstraction of it */ 00040 class QSocketNotifier; 00041 00042 /* 00043 * This is extending QIODevice's error codes 00044 * 00045 * According to qiodevice.h, the last error is IO_UnspecifiedError 00046 * These errors will never occur in functions declared in QIODevice 00047 * (except open, but you shouldn't call open) 00048 */ 00049 #define IO_ListenError (IO_UnspecifiedError+1) 00050 #define IO_AcceptError (IO_UnspecifiedError+2) 00051 #define IO_LookupError (IO_UnspecifiedError+3) 00052 00053 class KExtendedSocketPrivate; 00091 class KDECORE_EXPORT KExtendedSocket: public KBufferedIO // public QObject, public QIODevice 00092 { 00093 Q_OBJECT 00094 00095 public: 00099 enum Flags 00100 { 00101 /* socket address families */ 00102 /* 00103 * NOTE: if you change this, you have to change function valid_socket() as well 00104 * These values are hard coded! 00105 */ 00106 anySocket = 0x00, 00107 knownSocket = 0x01, 00108 unixSocket = knownSocket | 0x02, 00109 inetSocket = knownSocket | 0x04, 00110 ipv4Socket = inetSocket | 0x100, 00111 ipv6Socket = inetSocket | 0x200, 00112 00113 passiveSocket = 0x1000, /* passive socket (i.e., one that accepts connections) */ 00114 canonName = 0x2000, /* request that the canon name be found */ 00115 noResolve = 0x4000, /* do not attempt to resolve, treat as numeric host */ 00116 00117 streamSocket = 0x8000, /* request a streaming socket (e.g., TCP) */ 00118 datagramSocket = 0x10000, /* request a datagram socket (e.g., UDP) */ 00119 rawSocket = 0x20000, /* request a raw socket. This probably requires privileges */ 00120 00121 inputBufferedSocket = 0x200000, /* buffer input in this socket */ 00122 outputBufferedSocket = 0x400000, /* buffer output in this socket */ 00123 bufferedSocket = 0x600000 /* make this a fully buffered socket */ 00124 }; 00125 00131 enum SockStatus 00132 { 00133 // the numbers are scattered so that we leave room for future expansion 00134 error = -1, // invalid status! 00135 00136 nothing = 0, // no status, the class has just been created 00137 00138 lookupInProgress = 50, // lookup is in progress. Signals will be sent 00139 lookupDone = 70, // lookup has been done. Flags cannot be changed 00140 // from this point on 00141 00142 created = 100, // ::socket() has been called, a socket exists 00143 bound = 140, // socket has been bound 00144 00145 connecting = 200, // socket is connecting (not passiveSocket) 00146 connected = 220, // socket has connected (not passiveSocket) 00147 00148 listening = 200, // socket is listening (passiveSocket) 00149 accepting = 220, // socket is accepting (passiveSocket) 00150 00151 closing = 350, // socket is closing (delayed close) 00152 00153 done = 400 // socket has been closed 00154 }; 00155 00156 public: 00160 KExtendedSocket(); 00161 00178 KExtendedSocket(const QString& host, int port, int flags = 0); 00179 00196 KExtendedSocket(const QString& host, const QString& service, int flags = 0); 00197 00202 virtual ~KExtendedSocket(); 00203 00209 void reset(); 00210 00211 /* 00212 * --- status, flags and internal variables --- * 00213 */ 00214 00220 int socketStatus() const; 00221 00228 int systemError() const; 00229 00235 int setSocketFlags(int flags); 00236 00242 int socketFlags() const; 00243 00257 bool setHost(const QString& host); 00258 00263 QString host() const; 00264 00269 bool setPort(int port); 00270 00280 bool setPort(const QString& port); 00281 00286 QString port() const; 00287 00297 bool setAddress(const QString& host, int port); 00298 00308 bool setAddress(const QString& host, const QString& serv); 00309 00315 bool setBindHost(const QString& host); 00316 00321 bool unsetBindHost(); 00322 00327 QString bindHost() const; 00328 00334 bool setBindPort(int port); 00335 00341 bool setBindPort(const QString& service); 00342 00347 bool unsetBindPort(); 00348 00353 QString bindPort() const; 00354 00362 bool setBindAddress(const QString& host, int port); 00363 00371 bool setBindAddress(const QString& host, const QString& service); 00372 00378 bool unsetBindAddress(); 00379 00391 bool setTimeout(int secs, int usecs = 0); 00392 00397 timeval timeout() const; 00398 00407 bool setBlockingMode(bool enable); 00408 00413 bool blockingMode(); 00414 00424 bool setAddressReusable(bool enable); 00425 00430 bool addressReusable(); 00431 00450 bool setIPv6Only(bool enable); 00451 00458 bool isIPv6Only(); 00459 00477 virtual bool setBufferSize(int rsize, int wsize = -2); 00478 00484 const ::KSocketAddress *localAddress(); 00485 00492 const ::KSocketAddress *peerAddress(); 00493 00498 inline int fd() const 00499 { return sockfd; } 00500 00501 /* 00502 * -- socket creation -- * 00503 */ 00504 00512 virtual int lookup(); 00513 00532 virtual int startAsyncLookup(); 00533 00537 virtual void cancelAsyncLookup(); 00538 00546 virtual int listen(int N = 5); // 5 is arbitrary 00547 00562 virtual int accept(KExtendedSocket *&sock); 00563 00587 virtual int connect(); 00588 00603 virtual int startAsyncConnect(); 00604 00608 virtual void cancelAsyncConnect(); 00609 00620 virtual bool open(int mode = IO_Raw | IO_ReadWrite); 00621 00629 virtual void close(); 00630 00636 virtual void closeNow(); 00637 00651 virtual void release(); 00652 00653 /* 00654 * -- I/O -- 00655 */ 00656 00672 virtual void flush(); 00673 00678 virtual inline Q_ULONG size() const 00679 { return 0; } 00680 00685 virtual inline Q_ULONG at() const 00686 { return 0; } 00687 00693 virtual inline bool at(int i) 00694 { Q_UNUSED(i);return true; } 00695 00701 virtual inline bool atEnd() const 00702 { return false; } 00703 00733 virtual Q_LONG readBlock(char *data, Q_ULONG maxlen); 00734 00758 virtual Q_LONG writeBlock(const char *data, Q_ULONG len); 00759 00774 virtual int peekBlock(char *data, uint maxlen); 00775 00782 virtual int unreadBlock(const char *data, uint len); 00783 00793 virtual int bytesAvailable() const; 00794 00804 virtual int waitForMore(int msec); 00805 00810 virtual int getch(); 00811 00817 virtual int putch(int ch); 00818 00823 virtual int ungetch(int) 00824 { return -1; } 00825 00836 virtual void enableRead(bool enable); 00837 00847 virtual void enableWrite(bool enable); 00848 00849 signals: 00855 void lookupFinished(int count); 00856 00860 void connectionSuccess(); 00861 00867 void connectionFailed(int error); 00868 00874 void readyAccept(); 00875 00876 protected: 00877 int sockfd; // file descriptor of the socket 00878 00879 protected slots: 00880 00881 void socketActivityRead(); 00882 void socketActivityWrite(); 00883 void dnsResultsReady(); 00884 void startAsyncConnectSlot(); 00885 void connectionEvent(); 00886 00887 protected: 00888 00889 QSocketNotifier *readNotifier(); 00890 QSocketNotifier *writeNotifier(); 00891 00892 private: 00893 00894 // protection against accidental use 00895 KExtendedSocket(KExtendedSocket&); 00896 KExtendedSocket& operator=(KExtendedSocket&); 00897 00902 static int doLookup(const QString& host, const QString& serv, addrinfo& hint, 00903 kde_addrinfo** result); 00904 00905 protected: 00909 void setError(int errorkind, int error); 00910 00911 inline void cleanError() 00912 { setError(IO_Ok, 0); } 00913 00917 void setSocketStatus(int status); 00918 00919 public: 00933 static int resolve(sockaddr* sock, ksocklen_t len, QString& host, QString& port, int flags = 0) KDE_DEPRECATED; 00934 00947 static int resolve(::KSocketAddress* sock, QString& host, QString& port, int flags = 0) KDE_DEPRECATED; 00948 00969 static QPtrList<KAddressInfo> lookup(const QString& host, const QString& port, int flags = 0, int *error = 0) KDE_DEPRECATED; 00970 00977 static ::KSocketAddress *localAddress(int fd) KDE_DEPRECATED; 00978 00986 static ::KSocketAddress *peerAddress(int fd) KDE_DEPRECATED; 00987 00994 static QString strError(int code, int syserr); 00995 01005 static bool setAddressReusable(int fd, bool enable) KDE_DEPRECATED; 01006 01007 protected: 01008 virtual void virtual_hook( int id, void* data ); 01009 private: 01010 KExtendedSocketPrivate *d; 01011 01012 friend class KSocket; 01013 friend class KServerSocket; 01014 }; 01015 01022 class KDECORE_EXPORT KAddressInfo 01023 { 01024 private: 01025 addrinfo *ai; 01026 ::KSocketAddress *addr; 01027 01028 inline KAddressInfo() : ai(0), addr(0) 01029 { } 01030 01031 // KAddressInfo(addrinfo *ai); 01032 KAddressInfo(KAddressInfo&) { } 01033 KAddressInfo& operator=(KAddressInfo&) { return *this; } 01034 01035 public: 01036 ~KAddressInfo(); 01037 01042 inline KDE_DEPRECATED operator const ::KSocketAddress*() const 01043 { return addr; } 01044 01048 inline KDE_DEPRECATED operator const addrinfo&() const 01049 { return *ai; } 01050 01055 inline KDE_DEPRECATED operator const addrinfo*() const 01056 { return ai; } 01057 01063 inline KDE_DEPRECATED const ::KSocketAddress* address() const 01064 { return addr; } 01065 01070 int flags() const KDE_DEPRECATED; 01071 01076 int family() const KDE_DEPRECATED; 01077 01082 int socktype() const KDE_DEPRECATED; 01083 01088 int protocol() const KDE_DEPRECATED; 01089 01090 01096 const char* canonname() const KDE_DEPRECATED; 01097 01102 inline int length() const 01103 { if (addr) return addr->size(); return 0; } 01104 01105 friend class KExtendedSocket; 01106 }; 01107 01108 #endif //Q_OS_UNIX 01109 01110 #endif // KEXTSOCK_H