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_MOC_RUN 00026 #define Q_OS_UNIX 00027 #endif // Q_MOC_RUN 00028 00029 #ifdef Q_OS_UNIX 00030 00031 #include <sys/time.h> 00032 00033 #include <tqstring.h> 00034 #include <tqptrlist.h> 00035 #include <tqiodevice.h> 00036 00037 #include "kbufferedio.h" 00038 #include "ksockaddr.h" 00039 00040 /* External reference to netdb.h */ 00041 struct addrinfo; 00042 struct kde_addrinfo; 00043 class KAddressInfo; /* our abstraction of it */ 00044 class TQSocketNotifier; 00045 00046 /* 00047 * This is extending QIODevice's error codes 00048 * 00049 * According to tqiodevice.h, the last error is IO_UnspecifiedError 00050 * These errors will never occur in functions declared in QIODevice 00051 * (except open, but you shouldn't call open) 00052 */ 00053 #define IO_ListenError (IO_UnspecifiedError+1) 00054 #define IO_AcceptError (IO_UnspecifiedError+2) 00055 #define IO_LookupError (IO_UnspecifiedError+3) 00056 00057 class KExtendedSocketPrivate; 00095 class KDECORE_EXPORT KExtendedSocket: public KBufferedIO // public TQObject, public QIODevice 00096 { 00097 Q_OBJECT 00098 TQ_OBJECT 00099 00100 public: 00104 enum Flags 00105 { 00106 /* socket address families */ 00107 /* 00108 * NOTE: if you change this, you have to change function valid_socket() as well 00109 * These values are hard coded! 00110 */ 00111 anySocket = 0x00, 00112 knownSocket = 0x01, 00113 unixSocket = knownSocket | 0x02, 00114 inetSocket = knownSocket | 0x04, 00115 ipv4Socket = inetSocket | 0x100, 00116 ipv6Socket = inetSocket | 0x200, 00117 00118 passiveSocket = 0x1000, /* passive socket (i.e., one that accepts connections) */ 00119 canonName = 0x2000, /* request that the canon name be found */ 00120 noResolve = 0x4000, /* do not attempt to resolve, treat as numeric host */ 00121 00122 streamSocket = 0x8000, /* request a streaming socket (e.g., TCP) */ 00123 datagramSocket = 0x10000, /* request a datagram socket (e.g., UDP) */ 00124 rawSocket = 0x20000, /* request a raw socket. This probably requires privileges */ 00125 00126 inputBufferedSocket = 0x200000, /* buffer input in this socket */ 00127 outputBufferedSocket = 0x400000, /* buffer output in this socket */ 00128 bufferedSocket = 0x600000 /* make this a fully buffered socket */ 00129 }; 00130 00136 enum SockStatus 00137 { 00138 // the numbers are scattered so that we leave room for future expansion 00139 error = -1, // invalid status! 00140 00141 nothing = 0, // no status, the class has just been created 00142 00143 lookupInProgress = 50, // lookup is in progress. Signals will be sent 00144 lookupDone = 70, // lookup has been done. Flags cannot be changed 00145 // from this point on 00146 00147 created = 100, // ::socket() has been called, a socket exists 00148 bound = 140, // socket has been bound 00149 00150 connecting = 200, // socket is connecting (not passiveSocket) 00151 connected = 220, // socket has connected (not passiveSocket) 00152 00153 listening = 200, // socket is listening (passiveSocket) 00154 accepting = 220, // socket is accepting (passiveSocket) 00155 00156 closing = 350, // socket is closing (delayed close) 00157 00158 done = 400 // socket has been closed 00159 }; 00160 00161 public: 00165 KExtendedSocket(); 00166 00183 KExtendedSocket(const TQString& host, int port, int flags = 0); 00184 00201 KExtendedSocket(const TQString& host, const TQString& service, int flags = 0); 00202 00207 virtual ~KExtendedSocket(); 00208 00214 #ifdef USE_QT3 00215 void reset(); 00216 #endif // USE_QT3 00217 #ifdef USE_QT4 00218 bool reset(); 00219 #endif // USE_QT4 00220 00221 /* 00222 * --- status, flags and internal variables --- * 00223 */ 00224 00230 int socketStatus() const; 00231 00238 int systemError() const; 00239 00245 int setSocketFlags(int flags); 00246 00252 int socketFlags() const; 00253 00267 bool setHost(const TQString& host); 00268 00273 TQString host() const; 00274 00279 bool setPort(int port); 00280 00290 bool setPort(const TQString& port); 00291 00296 TQString port() const; 00297 00307 bool setAddress(const TQString& host, int port); 00308 00318 bool setAddress(const TQString& host, const TQString& serv); 00319 00325 bool setBindHost(const TQString& host); 00326 00331 bool unsetBindHost(); 00332 00337 TQString bindHost() const; 00338 00344 bool setBindPort(int port); 00345 00351 bool setBindPort(const TQString& service); 00352 00357 bool unsetBindPort(); 00358 00363 TQString bindPort() const; 00364 00372 bool setBindAddress(const TQString& host, int port); 00373 00381 bool setBindAddress(const TQString& host, const TQString& service); 00382 00388 bool unsetBindAddress(); 00389 00401 bool setTimeout(int secs, int usecs = 0); 00402 00407 timeval timeout() const; 00408 00417 bool setBlockingMode(bool enable); 00418 00423 bool blockingMode(); 00424 00434 bool setAddressReusable(bool enable); 00435 00440 bool addressReusable(); 00441 00460 bool setIPv6Only(bool enable); 00461 00468 bool isIPv6Only(); 00469 00487 virtual bool setBufferSize(int rsize, int wsize = -2); 00488 00494 const ::KSocketAddress *localAddress(); 00495 00502 const ::KSocketAddress *peerAddress(); 00503 00508 inline int fd() const 00509 { return sockfd; } 00510 00511 /* 00512 * -- socket creation -- * 00513 */ 00514 00522 virtual int lookup(); 00523 00542 virtual int startAsyncLookup(); 00543 00547 virtual void cancelAsyncLookup(); 00548 00556 virtual int listen(int N = 5); // 5 is arbitrary 00557 00572 virtual int accept(KExtendedSocket *&sock); 00573 00597 virtual int connect(); 00598 00613 virtual int startAsyncConnect(); 00614 00618 virtual void cancelAsyncConnect(); 00619 00630 virtual bool open(TQ_OpenMode mode = (TQ_OpenMode)(IO_Raw | IO_ReadWrite)); 00631 00639 virtual void close(); 00640 00646 virtual void closeNow(); 00647 00661 virtual void release(); 00662 00663 /* 00664 * -- I/O -- 00665 */ 00666 00682 virtual void flush(); 00683 00688 #ifdef USE_QT3 00689 virtual inline TQ_ULONG size() const 00690 #endif // USE_QT3 00691 #ifdef USE_QT4 00692 virtual inline qint64 size() const 00693 #endif // USE_QT4 00694 { return 0; } 00695 00700 virtual inline TQ_ULONG at() const 00701 { return 0; } 00702 00708 virtual inline bool at(int i) 00709 { Q_UNUSED(i);return true; } 00710 00716 virtual inline bool atEnd() const 00717 { return false; } 00718 00748 virtual TQT_TQIO_LONG tqreadBlock(char *data, TQT_TQIO_ULONG maxlen); 00749 00773 virtual TQT_TQIO_LONG tqwriteBlock(const char *data, TQT_TQIO_ULONG len); 00774 00789 virtual int peekBlock(char *data, uint maxlen); 00790 00797 virtual int unreadBlock(const char *data, uint len); 00798 00808 #ifdef USE_QT3 00809 virtual int bytesAvailable() const; 00810 #endif // USE_QT3 00811 #ifdef USE_QT4 00812 virtual qint64 bytesAvailable() const; 00813 #endif // USE_QT4 00814 00824 virtual int waitForMore(int msec); 00825 00830 virtual int getch(); 00831 00837 virtual int putch(int ch); 00838 00843 virtual int ungetch(int) 00844 { return -1; } 00845 00856 virtual void enableRead(bool enable); 00857 00867 virtual void enableWrite(bool enable); 00868 00869 signals: 00875 void lookupFinished(int count); 00876 00880 void connectionSuccess(); 00881 00887 void connectionFailed(int error); 00888 00894 void readyAccept(); 00895 00896 protected: 00897 int sockfd; // file descriptor of the socket 00898 00899 protected slots: 00900 00901 void socketActivityRead(); 00902 void socketActivityWrite(); 00903 void dnsResultsReady(); 00904 void startAsyncConnectSlot(); 00905 void connectionEvent(); 00906 00907 protected: 00908 00909 TQSocketNotifier *readNotifier(); 00910 TQSocketNotifier *writeNotifier(); 00911 00912 private: 00913 00914 // protection against accidental use 00915 KExtendedSocket(KExtendedSocket&); 00916 KExtendedSocket& operator=(KExtendedSocket&); 00917 00922 static int doLookup(const TQString& host, const TQString& serv, addrinfo& hint, 00923 kde_addrinfo** result); 00924 00925 protected: 00929 void setError(int errorkind, int error); 00930 00931 inline void cleanError() 00932 { setError(IO_Ok, 0); } 00933 00937 void setSocketStatus(int status); 00938 00939 public: 00953 static int resolve(sockaddr* sock, ksocklen_t len, TQString& host, TQString& port, int flags = 0) KDE_DEPRECATED; 00954 00967 static int resolve(::KSocketAddress* sock, TQString& host, TQString& port, int flags = 0) KDE_DEPRECATED; 00968 00989 static TQPtrList<KAddressInfo> lookup(const TQString& host, const TQString& port, int flags = 0, int *error = 0) KDE_DEPRECATED; 00990 00997 static ::KSocketAddress *localAddress(int fd) KDE_DEPRECATED; 00998 01006 static ::KSocketAddress *peerAddress(int fd) KDE_DEPRECATED; 01007 01014 static TQString strError(int code, int syserr); 01015 01025 static bool setAddressReusable(int fd, bool enable) KDE_DEPRECATED; 01026 01027 protected: 01028 virtual void virtual_hook( int id, void* data ); 01029 private: 01030 KExtendedSocketPrivate *d; 01031 01032 friend class KSocket; 01033 friend class KServerSocket; 01034 }; 01035 01042 class KDECORE_EXPORT KAddressInfo 01043 { 01044 private: 01045 addrinfo *ai; 01046 ::KSocketAddress *addr; 01047 01048 inline KAddressInfo() : ai(0), addr(0) 01049 { } 01050 01051 // KAddressInfo(addrinfo *ai); 01052 KAddressInfo(KAddressInfo&) { } 01053 KAddressInfo& operator=(KAddressInfo&) { return *this; } 01054 01055 public: 01056 ~KAddressInfo(); 01057 01062 inline KDE_DEPRECATED operator const ::KSocketAddress*() const 01063 { return addr; } 01064 01068 inline KDE_DEPRECATED operator const addrinfo&() const 01069 { return *ai; } 01070 01075 inline KDE_DEPRECATED operator const addrinfo*() const 01076 { return ai; } 01077 01083 inline KDE_DEPRECATED const ::KSocketAddress* address() const 01084 { return addr; } 01085 01090 int flags() const KDE_DEPRECATED; 01091 01096 int family() const KDE_DEPRECATED; 01097 01102 int socktype() const KDE_DEPRECATED; 01103 01108 int protocol() const KDE_DEPRECATED; 01109 01110 01116 const char* canonname() const KDE_DEPRECATED; 01117 01122 inline int length() const 01123 { if (addr) return addr->size(); return 0; } 01124 01125 friend class KExtendedSocket; 01126 }; 01127 01128 #endif //Q_OS_UNIX 01129 01130 #endif // KEXTSOCK_H