ksockaddr.h
/*
* This file is part of the KDE libraries
* Copyright (C) 2000-2003 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 KSOCKADDR_H
#define KSOCKADDR_H
#include <qobject.h>
#include <qcstring.h>
#include <qstring.h>
#include "kdelibs_export.h"
/*
* This file defines a class that envelopes most, if not all, socket addresses
*/
typedef unsigned ksocklen_t;
struct sockaddr;
class KExtendedSocket; // No need to define it fully
class KSocketAddressPrivate;
class KDECORE_EXPORT KSocketAddress: public QObject
{
Q_OBJECT
protected:
KSocketAddress() { init(); }
KSocketAddress(const sockaddr* sa, ksocklen_t size);
public:
virtual ~KSocketAddress();
virtual QString pretty() const;
const sockaddr* address() const
{ return data; }
virtual ksocklen_t size() const
{ return datasize; }
operator const sockaddr*() const
{ return data; }
int family() const;
inline int ianaFamily() const
{ return ianaFamily(family()); }
virtual bool isEqual(const KSocketAddress& other) const;
bool isEqual(const KSocketAddress* other) const
{ return isEqual(*other); }
bool operator==(const KSocketAddress& other) const
{ return isEqual(other); }
bool isCoreEqual(const KSocketAddress& other) const;
bool isCoreEqual(const KSocketAddress* other) const
{ return isCoreEqual(*other); }
virtual QString nodeName() const;
virtual QString serviceName() const;
protected:
sockaddr* data;
ksocklen_t datasize;
bool owndata;
private:
void init();
/* No copy constructor */
KSocketAddress(KSocketAddress&);
KSocketAddress& operator=(KSocketAddress&);
public:
static KSocketAddress* newAddress(const struct sockaddr *sa, ksocklen_t size);
static int ianaFamily(int af);
static int fromIanaFamily(int iana);
friend class KExtendedSocket;
protected:
virtual void virtual_hook( int id, void* data );
private:
KSocketAddressPrivate* d;
};
/*
* External definitions
* We need these for KInetSocketAddress
*/
struct sockaddr_in;
struct sockaddr_in6;
struct in_addr;
struct in6_addr;
class KInetSocketAddressPrivate;
class KDECORE_EXPORT KInetSocketAddress: public ::KSocketAddress
{
Q_OBJECT
public:
KInetSocketAddress();
KInetSocketAddress(const KInetSocketAddress&);
KInetSocketAddress(const sockaddr_in* sin, ksocklen_t len);
KInetSocketAddress(const sockaddr_in6* sin6, ksocklen_t len);
KInetSocketAddress(const in_addr& addr, unsigned short port);
KInetSocketAddress(const in6_addr& addr, unsigned short port);
KInetSocketAddress(const QString& addr, unsigned short port, int family = -1);
virtual ~KInetSocketAddress();
bool setAddress(const KInetSocketAddress& ksa);
bool setAddress(const sockaddr_in* sin, ksocklen_t len);
bool setAddress(const sockaddr_in6* sin6, ksocklen_t len);
bool setAddress(const in_addr& addr, unsigned short port);
bool setAddress(const in6_addr& addr, unsigned short port);
bool setAddress(const QString& addr, unsigned short port, int family = -1);
bool setHost(const in_addr& addr);
bool setHost(const in6_addr& addr);
bool setHost(const QString& addr, int family = -1);
bool setPort(unsigned short port);
bool setFamily(int family);
bool setFlowinfo(Q_UINT32 flowinfo);
bool setScopeId(int scopeid);
virtual QString pretty() const;
virtual QString nodeName() const;
// QString prettyHost() const;
virtual QString serviceName() const;
const sockaddr_in* addressV4() const;
const sockaddr_in6* addressV6() const;
in_addr hostV4() const;
in6_addr hostV6() const;
unsigned short port() const;
Q_UINT32 flowinfo() const;
int scopeId() const;
virtual ksocklen_t size() const; // should be socklen_t
/* comparation */
static bool areEqualInet(const KSocketAddress &s1, const KSocketAddress &s2, bool coreOnly);
static bool areEqualInet6(const KSocketAddress &s1, const KSocketAddress &s2, bool coreOnly);
/* operators */
operator const sockaddr_in*() const
{ return addressV4(); }
operator const sockaddr_in6*() const
{ return addressV6(); }
KInetSocketAddress& operator=(const KInetSocketAddress &other)
{ setAddress(other); return *this; }
private:
void fromV4();
void fromV6();
public:
static QString addrToString(int family, const void *addr);
static bool stringToAddr(int family, const char *text, void *dest);
friend class KExtendedSocket;
protected:
virtual void virtual_hook( int id, void* data );
private:
KInetSocketAddressPrivate* d;
};
extern const ::KInetSocketAddress addressAny, addressLoopback;
/*
* External definition KUnixSocketAddress
*/
struct sockaddr_un;
class KUnixSocketAddressPrivate;
class KDECORE_EXPORT KUnixSocketAddress: public ::KSocketAddress
{
Q_OBJECT
public:
KUnixSocketAddress();
KUnixSocketAddress(const sockaddr_un* raw_data, ksocklen_t size);
KUnixSocketAddress(QCString pathname);
virtual ~KUnixSocketAddress();
bool setAddress(const sockaddr_un* socket_address, ksocklen_t size);
bool setAddress(QCString path);
QCString pathname() const;
virtual QString pretty() const;
/*
* Returns the path in the form of a QString.
* This can be fed into KExtendedSocket.
* @return the path (can be QString::null).
* @see pathname()
*/
virtual QString serviceName() const;
const sockaddr_un* address() const;
operator const sockaddr_un*() const
{ return address(); }
static bool areEqualUnix(const KSocketAddress &s1, const KSocketAddress &s2, bool coreOnly);
private:
void init();
friend class KExtendedSocket;
protected:
virtual void virtual_hook( int id, void* data );
private:
KUnixSocketAddressPrivate* d;
};
#endif // KSOCKADDR_H
KSocketAddress::address
const sockaddr * address() const
Returns a sockaddr structure, for passing down to library functions.
Definition: ksockaddr.h:78
KInetSocketAddress
An Inet (IPv4 or IPv6) socket address.
Definition: ksockaddr.h:233
KSocketAddress::isCoreEqual
bool isCoreEqual(const KSocketAddress *other) const
Some sockets may differ in such things as services or port numbers, like Internet sockets...
Definition: ksockaddr.h:148
KSocketAddress::nodeName
virtual QString nodeName() const
Returns the node name of this socket, as KExtendedSocket::lookup expects as the first argument...
Definition: ksockaddr.cpp:196
KSocketAddress::operator==
bool operator==(const KSocketAddress &other) const
Overloaded == operator.
Definition: ksockaddr.h:123
KSocketAddress::size
virtual ksocklen_t size() const
Returns sockaddr structure size.
Definition: ksockaddr.h:85
KSocketAddress::KSocketAddress
KSocketAddress()
Creates an empty class.
Definition: ksockaddr.h:53
KSocketAddress::ianaFamily
int ianaFamily() const
Returns the IANA family number of this address.
Definition: ksockaddr.h:107
KSocketAddress
A socket address.
Definition: ksockaddr.h:46
KUnixSocketAddress
A Unix socket address.
Definition: ksockaddr.h:584
KInetSocketAddress::operator=
KInetSocketAddress & operator=(const KInetSocketAddress &other)
Sets this object to be the same as the other.
Definition: ksockaddr.h:528
KExtendedSocket
The extended socket class.
Definition: kextsock.h:91
KSocketAddress::serviceName
virtual QString serviceName() const
Returns the service name for this socket, as KExtendedSocket::lookup expects as the service argument...
Definition: ksockaddr.cpp:201
KSocketAddress::pretty
virtual QString pretty() const
Returns a string representation of this socket.
Definition: ksockaddr.cpp:110

KDE Home | KDE Accessibility Home | Description of Access Keys