ksocketaddress.h
/* -*- C++ -*-
* Copyright (C) 2003 Thiago Macieira <thiago.macieira@kdemail.net>
*
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef KSOCKETADDRESS_H
#define KSOCKETADDRESS_H
#include <qstring.h>
#include <qcstring.h>
#include <kdelibs_export.h>
struct sockaddr;
struct sockaddr_in;
struct sockaddr_in6;
struct sockaddr_un;
namespace KNetwork {
class KIpAddress;
class KSocketAddress;
class KInetSocketAddress;
class KUnixSocketAddress;
class KDECORE_EXPORT KIpAddress
{
public:
inline KIpAddress() : m_version(0)
{ }
inline KIpAddress(const KIpAddress& other)
{ *this = other; }
inline KIpAddress(const QString& addr)
{ setAddress(addr); }
inline KIpAddress(const char* addr)
{ setAddress(addr); }
inline KIpAddress(const void* addr, int version = 4)
{ setAddress(addr, version); }
inline KIpAddress(Q_UINT32 ip4addr)
{ setAddress(&ip4addr, 4); }
inline ~KIpAddress()
{ }
KIpAddress& operator =(const KIpAddress& other);
inline bool operator ==(const KIpAddress& other) const
{ return compare(other, true); }
bool compare(const KIpAddress& other, bool checkMapped = true) const;
inline int version() const
{ return m_version; }
inline bool isIPv4Addr() const
{ return version() == 4; }
inline bool isIPv6Addr() const
{ return version() == 6; }
bool setAddress(const QString& address);
bool setAddress(const char* address);
bool setAddress(const void* raw, int version = 4);
QString toString() const;
inline const void *addr() const
{ return m_data; }
inline Q_UINT32 IPv4Addr(bool convertMapped = true) const
{
return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0];
}
Q_UINT32 hostIPv4Addr(bool convertMapped = true) const;
public:
/*-- tests --*/
inline bool isUnspecified() const
{ return version() == 0 ? true : (*this == anyhostV4 || *this == anyhostV6); }
inline bool isLocalhost() const
{ return version() == 0 ? false : (*this == localhostV4 || *this == localhostV6); }
inline bool isLoopback() const
{ return isLocalhost(); }
inline bool isClassA() const
{ return version() != 4 ? false : (hostIPv4Addr() & 0x80000000) == 0; }
inline bool isClassB() const
{ return version() != 4 ? false : (hostIPv4Addr() & 0xc0000000) == 0x80000000; }
inline bool isClassC() const
{ return version() != 4 ? false : (hostIPv4Addr() & 0xe0000000) == 0xc0000000; }
inline bool isClassD() const
{ return version() != 4 ? false : (hostIPv4Addr() & 0xf0000000) == 0xe0000000; }
inline bool isMulticast() const
{
if (version() == 4) return isClassD();
if (version() == 6) return ((Q_UINT8*)addr())[0] == 0xff;
return false;
}
inline bool isLinkLocal() const
{
if (version() != 6) return false;
Q_UINT8* addr = (Q_UINT8*)this->addr();
return (addr[0] & 0xff) == 0xfe &&
(addr[1] & 0xc0) == 0x80;
}
inline bool isSiteLocal() const
{
if (version() != 6) return false;
Q_UINT8* addr = (Q_UINT8*)this->addr();
return (addr[0] & 0xff) == 0xfe &&
(addr[1] & 0xc0) == 0xc0;
}
inline bool isGlobal() const
{ return version() != 6 ? false : !(isMulticast() || isLinkLocal() || isSiteLocal()); }
inline bool isV4Mapped() const
{
if (version() != 6) return false;
Q_UINT32* addr = (Q_UINT32*)this->addr();
return addr[0] == 0 && addr[1] == 0 &&
((Q_UINT16*)&addr[2])[0] == 0 &&
((Q_UINT16*)&addr[2])[1] == 0xffff;
}
inline bool isV4Compat() const
{
if (version() != 6 || isLocalhost()) return false;
Q_UINT32* addr = (Q_UINT32*)this->addr();
return addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] != 0;
}
inline bool isMulticastNodeLocal() const
{ return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x1; }
inline bool isMulticastLinkLocal() const
{ return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x2; }
inline bool isMulticastSiteLocal() const
{ return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x5; }
inline bool isMulticastOrgLocal() const
{ return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x8; }
inline bool isMulticastGlobal() const
{ return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0xe; }
protected:
Q_UINT32 m_data[4]; // 16 bytes, needed for an IPv6 address
char m_version;
public:
static const KIpAddress localhostV4;
static const KIpAddress anyhostV4;
static const KIpAddress localhostV6;
static const KIpAddress anyhostV6;
};
class KSocketAddressData;
class KDECORE_EXPORT KSocketAddress
{
public:
KSocketAddress();
KSocketAddress(const sockaddr* sa, Q_UINT16 len);
KSocketAddress(const KSocketAddress& other);
virtual ~KSocketAddress();
KSocketAddress& operator =(const KSocketAddress& other);
const sockaddr* address() const;
sockaddr* address();
KSocketAddress& setAddress(const sockaddr *sa, Q_UINT16 len);
inline operator const sockaddr*() const
{ return address(); }
Q_UINT16 length() const;
KSocketAddress& setLength(Q_UINT16 len);
int family() const;
virtual KSocketAddress& setFamily(int family);
inline int ianaFamily() const
{ return ianaFamily(family()); }
bool operator ==(const KSocketAddress& other) const;
virtual QString nodeName() const;
virtual QString serviceName() const;
virtual QString toString() const;
KInetSocketAddress& asInet();
KInetSocketAddress asInet() const;
KUnixSocketAddress& asUnix();
KUnixSocketAddress asUnix() const;
protected:
KSocketAddressData *d;
KSocketAddress(KSocketAddressData* d);
public: // static
static int ianaFamily(int af);
static int fromIanaFamily(int iana);
};
class KDECORE_EXPORT KInetSocketAddress: public KSocketAddress
{
friend class KSocketAddress;
public:
KInetSocketAddress();
KInetSocketAddress(const sockaddr* sa, Q_UINT16 len);
KInetSocketAddress(const KIpAddress& host, Q_UINT16 port);
KInetSocketAddress(const KInetSocketAddress& other);
KInetSocketAddress(const KSocketAddress& other);
virtual ~KInetSocketAddress();
KInetSocketAddress& operator =(const KInetSocketAddress& other);
inline operator const sockaddr_in*() const
{ return (const sockaddr_in*)address(); }
inline operator const sockaddr_in6*() const
{ return (const sockaddr_in6*)address(); }
int ipVersion() const;
KIpAddress ipAddress() const;
KInetSocketAddress& setHost(const KIpAddress& addr);
Q_UINT16 port() const;
KInetSocketAddress& setPort(Q_UINT16 port);
KInetSocketAddress& makeIPv4();
KInetSocketAddress& makeIPv6();
Q_UINT32 flowinfo() const;
KInetSocketAddress& setFlowinfo(Q_UINT32 flowinfo);
int scopeId() const;
KInetSocketAddress& setScopeId(int scopeid);
protected:
KInetSocketAddress(KSocketAddressData* d);
private:
void update();
};
/*
* External definition
*/
class KDECORE_EXPORT KUnixSocketAddress: public KSocketAddress
{
friend class KSocketAddress;
public:
KUnixSocketAddress();
KUnixSocketAddress(const sockaddr* sa, Q_UINT16 len);
KUnixSocketAddress(const KUnixSocketAddress& other);
KUnixSocketAddress(const QString& pathname);
virtual ~KUnixSocketAddress();
KUnixSocketAddress& operator =(const KUnixSocketAddress& other);
inline operator const sockaddr_un*() const
{ return (const sockaddr_un*)address(); }
QString pathname() const;
KUnixSocketAddress& setPathname(const QString& path);
protected:
KUnixSocketAddress(KSocketAddressData* d);
};
} // namespace KNetwork
#endif
KNetwork::KIpAddress::anyhostV6
static const KIpAddress anyhostV6
the any host or undefined address in IPv6 (::)
Definition: ksocketaddress.h:411
KNetwork::KUnixSocketAddress
A Unix (local) socket address.
Definition: ksocketaddress.h:840
KNetwork::KInetSocketAddress
an Internet socket address
Definition: ksocketaddress.h:651
KNetwork::KIpAddress::isSiteLocal
bool isSiteLocal() const
Returns true if this is an IPv6 site-local address.
Definition: ksocketaddress.h:331
KNetwork::KIpAddress
An IP address.
Definition: ksocketaddress.h:62
KNetwork::KIpAddress::isV4Compat
bool isV4Compat() const
Returns true if this is a v4-compat IPv6 address.
Definition: ksocketaddress.h:360
KNetwork::KIpAddress::isMulticastSiteLocal
bool isMulticastSiteLocal() const
Returns true if this is an IPv6 site-local multicast address.
Definition: ksocketaddress.h:382
KSocketAddress::address
const sockaddr * address() const
Returns a sockaddr structure, for passing down to library functions.
Definition: ksockaddr.h:78
KNetwork::KIpAddress::isMulticastNodeLocal
bool isMulticastNodeLocal() const
Returns true if this is an IPv6 node-local multicast address.
Definition: ksocketaddress.h:370
KNetwork::KIpAddress::isLoopback
bool isLoopback() const
This is an alias for isLocalhost.
Definition: ksocketaddress.h:268
KInetSocketAddress
An Inet (IPv4 or IPv6) socket address.
Definition: ksockaddr.h:233
KNetwork::KSocketAddress
A generic socket address.
Definition: ksocketaddress.h:423
KNetwork::KIpAddress::isClassB
bool isClassB() const
Returns true if this is an IPv4 class B address, i.e., one from 128.0.0.0 to 191.255.255.255.
Definition: ksocketaddress.h:286
KNetwork::KIpAddress::isUnspecified
bool isUnspecified() const
Returns true if this is the IPv4 or IPv6 unspecified address.
Definition: ksocketaddress.h:256
KNetwork::KIpAddress::localhostV4
static const KIpAddress localhostV4
localhost in IPv4 (127.0.0.1)
Definition: ksocketaddress.h:404
KNetwork::KIpAddress::isMulticastLinkLocal
bool isMulticastLinkLocal() const
Returns true if this is an IPv6 link-local multicast address.
Definition: ksocketaddress.h:376
KNetwork::KIpAddress::isLinkLocal
bool isLinkLocal() const
Returns true if this is an IPv6 link-local address.
Definition: ksocketaddress.h:320
KNetwork::KIpAddress::addr
const void * addr() const
Returns a pointer to binary raw data representing the address.
Definition: ksocketaddress.h:220
KNetwork::KIpAddress::isClassD
bool isClassD() const
Returns true if this is an IPv4 class D (a.k.a.
Definition: ksocketaddress.h:304
KNetwork::KIpAddress::isClassC
bool isClassC() const
Returns true if this is an IPv4 class C address, i.e., one from 192.0.0.0 to 223.255.255.255.
Definition: ksocketaddress.h:295
KNetwork::KIpAddress::KIpAddress
KIpAddress()
Default constructor.
Definition: ksocketaddress.h:69
KNetwork::KIpAddress::anyhostV4
static const KIpAddress anyhostV4
the any host or undefined address in IPv4 (0.0.0.0)
Definition: ksocketaddress.h:406
KNetwork::KIpAddress::isIPv6Addr
bool isIPv6Addr() const
Returns true if this is an IPv6 address.
Definition: ksocketaddress.h:183
KNetwork::KIpAddress::IPv4Addr
Q_UINT32 IPv4Addr(bool convertMapped=true) const
This is a convenience function.
Definition: ksocketaddress.h:235
KNetwork::KSocketAddress::ianaFamily
int ianaFamily() const
Returns the IANA family number of this address.
Definition: ksocketaddress.h:550
KNetwork::KIpAddress::~KIpAddress
~KIpAddress()
Destructor.
Definition: ksocketaddress.h:131
KNetwork::KIpAddress::isMulticastOrgLocal
bool isMulticastOrgLocal() const
Returns true if this is an IPv6 organisational-local multicast address.
Definition: ksocketaddress.h:388
KNetwork::KIpAddress::KIpAddress
KIpAddress(Q_UINT32 ip4addr)
This is a convenience constructor.
Definition: ksocketaddress.h:122
KNetwork::KIpAddress::isV4Mapped
bool isV4Mapped() const
Returns true if this is a v4-mapped IPv6 address.
Definition: ksocketaddress.h:348
KSocketAddress
A socket address.
Definition: ksockaddr.h:46
KUnixSocketAddress
A Unix socket address.
Definition: ksockaddr.h:584
KNetwork::KIpAddress::KIpAddress
KIpAddress(const void *addr, int version=4)
Creates an object from the given raw data and IP version.
Definition: ksocketaddress.h:109
KNetwork::KIpAddress::KIpAddress
KIpAddress(const char *addr)
Creates an object from the given string representation.
Definition: ksocketaddress.h:100
KNetwork::KIpAddress::isMulticastGlobal
bool isMulticastGlobal() const
Returns true if this is an IPv6 global multicast address.
Definition: ksocketaddress.h:394
KNetwork::KIpAddress::version
int version() const
Retrieves the IP version in this object.
Definition: ksocketaddress.h:171
KNetwork::KIpAddress::isMulticast
bool isMulticast() const
Returns true if this is a multicast address, be it IPv4 or IPv6.
Definition: ksocketaddress.h:310
KNetwork::KIpAddress::isClassA
bool isClassA() const
Returns true if this is an IPv4 class A address, i.e., from 0.0.0.0 to 127.255.255.255.
Definition: ksocketaddress.h:277
KNetwork::KIpAddress::KIpAddress
KIpAddress(const QString &addr)
Creates an object from the given string representation.
Definition: ksocketaddress.h:90
KNetwork::KIpAddress::isIPv4Addr
bool isIPv4Addr() const
Returns true if this is an IPv4 address.
Definition: ksocketaddress.h:177
KNetwork::KIpAddress::localhostV6
static const KIpAddress localhostV6
localhost in IPv6 (::1)
Definition: ksocketaddress.h:409
KNetwork::KIpAddress::isGlobal
bool isGlobal() const
Returns true if this is a global IPv6 address.
Definition: ksocketaddress.h:342
KNetwork::KIpAddress::isLocalhost
bool isLocalhost() const
Returns true if this is either the IPv4 or the IPv6 localhost address.
Definition: ksocketaddress.h:262
KNetwork::KIpAddress::KIpAddress
KIpAddress(const KIpAddress &other)
Copy constructor.
Definition: ksocketaddress.h:80

KDE Home | KDE Accessibility Home | Description of Access Keys