kextsock.h
/*
* This file is part of the KDE libraries
* Copyright (C) 2000-2004 Thiago Macieira <thiago.macieira@kdemail.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KEXTSOCK_H
#define KEXTSOCK_H
#include "kdelibs_export.h"
#ifdef Q_OS_UNIX
#include <sys/time.h>
#include <qstring.h>
#include <qptrlist.h>
#include <qiodevice.h>
#include "kbufferedio.h"
#include "ksockaddr.h"
/* External reference to netdb.h */
struct addrinfo;
struct kde_addrinfo;
class KAddressInfo; /* our abstraction of it */
class QSocketNotifier;
/*
* This is extending QIODevice's error codes
*
* According to qiodevice.h, the last error is IO_UnspecifiedError
* These errors will never occur in functions declared in QIODevice
* (except open, but you shouldn't call open)
*/
#define IO_ListenError (IO_UnspecifiedError+1)
#define IO_AcceptError (IO_UnspecifiedError+2)
#define IO_LookupError (IO_UnspecifiedError+3)
class KExtendedSocketPrivate;
class KDECORE_EXPORT KExtendedSocket: public KBufferedIO // public QObject, public QIODevice
{
Q_OBJECT
public:
enum Flags
{
/* socket address families */
/*
* NOTE: if you change this, you have to change function valid_socket() as well
* These values are hard coded!
*/
anySocket = 0x00,
knownSocket = 0x01,
unixSocket = knownSocket | 0x02,
inetSocket = knownSocket | 0x04,
ipv4Socket = inetSocket | 0x100,
ipv6Socket = inetSocket | 0x200,
passiveSocket = 0x1000, /* passive socket (i.e., one that accepts connections) */
canonName = 0x2000, /* request that the canon name be found */
noResolve = 0x4000, /* do not attempt to resolve, treat as numeric host */
streamSocket = 0x8000, /* request a streaming socket (e.g., TCP) */
datagramSocket = 0x10000, /* request a datagram socket (e.g., UDP) */
rawSocket = 0x20000, /* request a raw socket. This probably requires privileges */
inputBufferedSocket = 0x200000, /* buffer input in this socket */
outputBufferedSocket = 0x400000, /* buffer output in this socket */
bufferedSocket = 0x600000 /* make this a fully buffered socket */
};
enum SockStatus
{
// the numbers are scattered so that we leave room for future expansion
error = -1, // invalid status!
nothing = 0, // no status, the class has just been created
lookupInProgress = 50, // lookup is in progress. Signals will be sent
lookupDone = 70, // lookup has been done. Flags cannot be changed
// from this point on
created = 100, // ::socket() has been called, a socket exists
bound = 140, // socket has been bound
connecting = 200, // socket is connecting (not passiveSocket)
connected = 220, // socket has connected (not passiveSocket)
listening = 200, // socket is listening (passiveSocket)
accepting = 220, // socket is accepting (passiveSocket)
closing = 350, // socket is closing (delayed close)
done = 400 // socket has been closed
};
public:
KExtendedSocket();
KExtendedSocket(const QString& host, int port, int flags = 0);
KExtendedSocket(const QString& host, const QString& service, int flags = 0);
virtual ~KExtendedSocket();
void reset();
/*
* --- status, flags and internal variables --- *
*/
int socketStatus() const;
int systemError() const;
int setSocketFlags(int flags);
int socketFlags() const;
bool setHost(const QString& host);
QString host() const;
bool setPort(int port);
bool setPort(const QString& port);
QString port() const;
bool setAddress(const QString& host, int port);
bool setAddress(const QString& host, const QString& serv);
bool setBindHost(const QString& host);
bool unsetBindHost();
QString bindHost() const;
bool setBindPort(int port);
bool setBindPort(const QString& service);
bool unsetBindPort();
QString bindPort() const;
bool setBindAddress(const QString& host, int port);
bool setBindAddress(const QString& host, const QString& service);
bool unsetBindAddress();
bool setTimeout(int secs, int usecs = 0);
timeval timeout() const;
bool setBlockingMode(bool enable);
bool blockingMode();
bool setAddressReusable(bool enable);
bool addressReusable();
bool setIPv6Only(bool enable);
bool isIPv6Only();
virtual bool setBufferSize(int rsize, int wsize = -2);
const ::KSocketAddress *localAddress();
const ::KSocketAddress *peerAddress();
inline int fd() const
{ return sockfd; }
/*
* -- socket creation -- *
*/
virtual int lookup();
virtual int startAsyncLookup();
virtual void cancelAsyncLookup();
virtual int listen(int N = 5); // 5 is arbitrary
virtual int accept(KExtendedSocket *&sock);
virtual int connect();
virtual int startAsyncConnect();
virtual void cancelAsyncConnect();
virtual bool open(int mode = IO_Raw | IO_ReadWrite);
virtual void close();
virtual void closeNow();
virtual void release();
/*
* -- I/O --
*/
virtual void flush();
virtual inline Q_ULONG size() const
{ return 0; }
virtual inline Q_ULONG at() const
{ return 0; }
virtual inline bool at(int i)
{ Q_UNUSED(i);return true; }
virtual inline bool atEnd() const
{ return false; }
virtual Q_LONG readBlock(char *data, Q_ULONG maxlen);
virtual Q_LONG writeBlock(const char *data, Q_ULONG len);
virtual int peekBlock(char *data, uint maxlen);
virtual int unreadBlock(const char *data, uint len);
virtual int bytesAvailable() const;
virtual int waitForMore(int msec);
virtual int getch();
virtual int putch(int ch);
virtual int ungetch(int)
{ return -1; }
virtual void enableRead(bool enable);
virtual void enableWrite(bool enable);
signals:
void lookupFinished(int count);
void connectionSuccess();
void connectionFailed(int error);
void readyAccept();
protected:
int sockfd; // file descriptor of the socket
protected slots:
void socketActivityRead();
void socketActivityWrite();
void dnsResultsReady();
void startAsyncConnectSlot();
void connectionEvent();
protected:
QSocketNotifier *readNotifier();
QSocketNotifier *writeNotifier();
private:
// protection against accidental use
KExtendedSocket(KExtendedSocket&);
KExtendedSocket& operator=(KExtendedSocket&);
static int doLookup(const QString& host, const QString& serv, addrinfo& hint,
kde_addrinfo** result);
protected:
void setError(int errorkind, int error);
inline void cleanError()
{ setError(IO_Ok, 0); }
void setSocketStatus(int status);
public:
static int resolve(sockaddr* sock, ksocklen_t len, QString& host, QString& port, int flags = 0) KDE_DEPRECATED;
static int resolve(::KSocketAddress* sock, QString& host, QString& port, int flags = 0) KDE_DEPRECATED;
static QPtrList<KAddressInfo> lookup(const QString& host, const QString& port, int flags = 0, int *error = 0) KDE_DEPRECATED;
static ::KSocketAddress *localAddress(int fd) KDE_DEPRECATED;
static ::KSocketAddress *peerAddress(int fd) KDE_DEPRECATED;
static QString strError(int code, int syserr);
static bool setAddressReusable(int fd, bool enable) KDE_DEPRECATED;
protected:
virtual void virtual_hook( int id, void* data );
private:
KExtendedSocketPrivate *d;
friend class KSocket;
friend class KServerSocket;
};
class KDECORE_EXPORT KAddressInfo
{
private:
addrinfo *ai;
::KSocketAddress *addr;
inline KAddressInfo() : ai(0), addr(0)
{ }
// KAddressInfo(addrinfo *ai);
KAddressInfo(KAddressInfo&) { }
KAddressInfo& operator=(KAddressInfo&) { return *this; }
public:
~KAddressInfo();
inline KDE_DEPRECATED operator const ::KSocketAddress*() const
{ return addr; }
inline KDE_DEPRECATED operator const addrinfo&() const
{ return *ai; }
inline KDE_DEPRECATED operator const addrinfo*() const
{ return ai; }
inline KDE_DEPRECATED const ::KSocketAddress* address() const
{ return addr; }
int flags() const KDE_DEPRECATED;
int family() const KDE_DEPRECATED;
int socktype() const KDE_DEPRECATED;
int protocol() const KDE_DEPRECATED;
const char* canonname() const KDE_DEPRECATED;
inline int length() const
{ if (addr) return addr->size(); return 0; }
friend class KExtendedSocket;
};
#endif //Q_OS_UNIX
#endif // KEXTSOCK_H
KExtendedSocket::ungetch
virtual int ungetch(int)
Unreads one character from the stream.
Definition: kextsock.h:823
flush
kndbgstream & flush(kndbgstream &s)
Does nothing.
Definition: kdebug.h:589
KAsyncIO::enableWrite
virtual void enableWrite(bool enable)=0
Toggles the emission of the readyWrite() signal whenever the device is ready for writing.
KExtendedSocket::at
virtual bool at(int i)
Returns true if we are at position.
Definition: kextsock.h:693
KBufferedIO::closeNow
virtual void closeNow()=0
Closes the stream now, discarding the contents of the write buffer.
KExtendedSocket::fd
int fd() const
Returns the file descriptor.
Definition: kextsock.h:498
KBufferedIO
This abstract class implements basic functionality for buffered input/output.
Definition: kbufferedio.h:56
KBufferedIO::waitForMore
virtual int waitForMore(int msec)=0
Waits for more data to be available and returns the amount of available data then.
KBufferedIO::bytesAvailable
virtual int bytesAvailable() const
Returns the number of bytes available for reading in the read buffer.
Definition: kbufferedio.cpp:114
KBufferedIO::peekBlock
virtual int peekBlock(char *data, uint maxlen)=0
Reads into the user buffer at most maxlen bytes, but does not consume that data from the read buffer...
KAddressInfo
Definition: kextsock.h:1022
KExtendedSocket::at
virtual Q_ULONG at() const
Returns relative position from start.
Definition: kextsock.h:685
KSocketAddress::size
virtual ksocklen_t size() const
Returns sockaddr structure size.
Definition: ksockaddr.h:85
KBufferedIO::unreadBlock
virtual int unreadBlock(const char *data, uint len)
Unreads some data.
Definition: kbufferedio.cpp:155
KBufferedIO::setBufferSize
virtual bool setBufferSize(int rsize, int wsize=-2)
Sets the internal buffer size to value.
Definition: kbufferedio.cpp:104
KSocketAddress
A socket address.
Definition: ksockaddr.h:46
KExtendedSocket::size
virtual Q_ULONG size() const
Returns length of this socket.
Definition: kextsock.h:678
KAddressInfo::address
KDE_DEPRECATEDconst::KSocketAddress * address() const
Returns the KAddressInfo's KSocketAddress.
Definition: kextsock.h:1063
KExtendedSocket
The extended socket class.
Definition: kextsock.h:91
KSocket
A TCP/IP client socket.
Definition: ksock.h:87
KExtendedSocket::SockStatus
SockStatus
status of the class The status are sequential.
Definition: kextsock.h:131
KServerSocket
Monitors a port for incoming TCP/IP connections.
Definition: ksock.h:250
KExtendedSocket::Flags
Flags
flags that can be passed down to the member functions
Definition: kextsock.h:99
KAsyncIO::enableRead
virtual void enableRead(bool enable)=0
Toggles the emission of the readyRead() signal whenever the device is ready for reading.
KExtendedSocket::atEnd
virtual bool atEnd() const
Returns true if we are at the end.
Definition: kextsock.h:701

KDE Home | KDE Accessibility Home | Description of Access Keys