• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • sources
  • kde-4.12
  • kdelibs
  • kdecore
  • network
k3socketaddress.h
Go to the documentation of this file.
1 //krazy:excludeall=dpointer,inline (lightweight classes; kde3 support)
2 /* -*- C++ -*-
3  * Copyright (C) 2003,2005 Thiago Macieira <thiago@kde.org>
4  *
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #ifndef KSOCKETADDRESS_H
27 #define KSOCKETADDRESS_H
28 
29 #include <kdecore_export.h>
30 #include <QtCore/QByteArray>
31 
32 struct sockaddr;
33 struct sockaddr_in;
34 struct sockaddr_in6;
35 struct sockaddr_un;
36 
37 namespace KNetwork {
38 
39 class KIpAddress;
40 class KSocketAddress;
41 class KInetSocketAddress;
42 class KUnixSocketAddress;
43 
62 class KDECORE_EXPORT_DEPRECATED KIpAddress
63 {
64 public:
69  inline KIpAddress() : m_version(0)
70  { }
71 
80  inline KIpAddress(const KIpAddress& other)
81  { *this = other; }
82 
90  inline KIpAddress(const QString& addr)
91  { setAddress(addr); }
92 
100  inline KIpAddress(const char* addr)
101  { setAddress(addr); }
102 
109  inline KIpAddress(const void* addr, int version = 4)
110  { setAddress(addr, version); }
111 
122  inline KIpAddress(quint32 ip4addr)
123  { setAddress(&ip4addr, 4); }
124 
131  inline ~KIpAddress()
132  { }
133 
141  KIpAddress& operator =(const KIpAddress& other);
142 
148  inline bool operator ==(const KIpAddress& other) const
149  { return compare(other, true); }
150 
164  bool compare(const KIpAddress& other, bool checkMapped = true) const;
165 
171  inline int version() const
172  { return m_version; }
173 
177  inline bool isIPv4Addr() const
178  { return version() == 4; }
179 
183  inline bool isIPv6Addr() const
184  { return version() == 6; }
185 
192  bool setAddress(const QString& address);
193 
200  bool setAddress(const char* address);
201 
210  bool setAddress(const void* raw, int version = 4);
211 
215  QString toString() const;
216 
220  inline const void *addr() const
221  { return m_data; }
222 
236  inline quint32 IPv4Addr(bool convertMapped = true) const
237  {
238  return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0];
239  }
240 
241  /*-- tests --*/
242 
246  inline bool isUnspecified() const
247  { return version() == 0 ? true : (*this == anyhostV4 || *this == anyhostV6); }
248 
252  inline bool isLocalhost() const
253  { return version() == 0 ? false : (*this == localhostV4 || *this == localhostV6); }
254 
258  inline bool isLoopback() const
259  { return isLocalhost(); }
260 
267  inline bool isClassA() const
268  { return version() != 4 ? false : (IPv4Addr() & 0x80000000) == 0; }
269 
276  inline bool isClassB() const
277  { return version() != 4 ? false : (IPv4Addr() & 0xc0000000) == 0x80000000; }
278 
285  inline bool isClassC() const
286  { return version() != 4 ? false : (IPv4Addr() & 0xe0000000) == 0xc0000000; }
287 
294  inline bool isClassD() const
295  { return version() != 4 ? false : (IPv4Addr() & 0xf0000000) == 0xe0000000; }
296 
300  inline bool isMulticast() const
301  {
302  if (version() == 4) return isClassD();
303  if (version() == 6) return ((quint8*)addr())[0] == 0xff;
304  return false;
305  }
306 
310  inline bool isLinkLocal() const
311  {
312  if (version() != 6) return false;
313  quint8* addr = (quint8*)this->addr();
314  return (addr[0] & 0xff) == 0xfe &&
315  (addr[1] & 0xc0) == 0x80;
316  }
317 
321  inline bool isSiteLocal() const
322  {
323  if (version() != 6) return false;
324  quint8* addr = (quint8*)this->addr();
325  return (addr[0] & 0xff) == 0xfe &&
326  (addr[1] & 0xc0) == 0xc0;
327  }
328 
332  inline bool isGlobal() const
333  { return version() != 6 ? false : !(isMulticast() || isLinkLocal() || isSiteLocal()); }
334 
338  inline bool isV4Mapped() const
339  {
340  if (version() != 6) return false;
341  quint32* addr = (quint32*)this->addr();
342  return addr[0] == 0 && addr[1] == 0 &&
343  ((quint16*)&addr[2])[0] == 0 &&
344  ((quint16*)&addr[2])[1] == 0xffff;
345  }
346 
350  inline bool isV4Compat() const
351  {
352  if (version() != 6 || isLocalhost()) return false;
353  quint32* addr = (quint32*)this->addr();
354  return addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] != 0;
355  }
356 
360  inline bool isMulticastNodeLocal() const
361  { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x1; }
362 
366  inline bool isMulticastLinkLocal() const
367  { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x2; }
368 
372  inline bool isMulticastSiteLocal() const
373  { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x5; }
374 
378  inline bool isMulticastOrgLocal() const
379  { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x8; }
380 
384  inline bool isMulticastGlobal() const
385  { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0xe; }
386 
387 protected:
388  quint32 m_data[4]; // 16 bytes, needed for an IPv6 address
389 
390  char m_version;
391 
392 public:
394  static const KIpAddress localhostV4;
396  static const KIpAddress anyhostV4;
397 
399  static const KIpAddress localhostV6;
401  static const KIpAddress anyhostV6;
402 };
403 
404 
405 class KSocketAddressData;
414 class KDECORE_EXPORT_DEPRECATED KSocketAddress //krazy:exclude=dpointer (we got one, just not called Private)
415 {
416 public:
422  KSocketAddress();
423 
431  KSocketAddress(const sockaddr* sa, quint16 len);
432 
441  KSocketAddress(const KSocketAddress& other);
442 
446  virtual ~KSocketAddress();
447 
454  KSocketAddress& operator =(const KSocketAddress& other);
455 
463  const sockaddr* address() const;
464 
475  sockaddr* address();
476 
484  KSocketAddress& setAddress(const sockaddr *sa, quint16 len);
485 
490  inline operator const sockaddr*() const
491  { return address(); }
492 
496  quint16 length() const;
497 
518  KSocketAddress& setLength(quint16 len);
519 
524  int family() const;
525 
534  virtual KSocketAddress& setFamily(int family);
535 
541  inline int ianaFamily() const
542  { return ianaFamily(family()); }
543 
552  bool operator ==(const KSocketAddress& other) const;
553 
563  virtual QString nodeName() const;
564 
574  virtual QString serviceName() const;
575 
582  virtual QString toString() const;
583 
588  KInetSocketAddress& asInet();
589 
593  KInetSocketAddress asInet() const;
594 
599  KUnixSocketAddress& asUnix();
600 
604  KUnixSocketAddress asUnix() const;
605 
606 protected:
609  KSocketAddressData *d;
610 
613  KSocketAddress(KSocketAddressData* d);
614 
615 public: // static
623  static int ianaFamily(int af);
624 
629  static int fromIanaFamily(int iana);
630 };
631 
632 
643 class KDECORE_EXPORT_DEPRECATED KInetSocketAddress: public KSocketAddress
644 {
645  friend class KSocketAddress;
646 public:
650  KInetSocketAddress();
651 
661  KInetSocketAddress(const sockaddr* sa, quint16 len);
662 
669  KInetSocketAddress(const KIpAddress& host, quint16 port);
670 
678  KInetSocketAddress(const KInetSocketAddress& other);
679 
688  KInetSocketAddress(const KSocketAddress& other);
689 
693  virtual ~KInetSocketAddress();
694 
702  KInetSocketAddress& operator =(const KInetSocketAddress& other);
703 
707  inline operator const sockaddr_in*() const
708  { return (const sockaddr_in*)address(); }
709 
713  inline operator const sockaddr_in6*() const
714  { return (const sockaddr_in6*)address(); }
715 
721  int ipVersion() const;
722 
726  KIpAddress ipAddress() const;
727 
737  KInetSocketAddress& setHost(const KIpAddress& addr);
738 
745  quint16 port() const;
746 
754  KInetSocketAddress& setPort(quint16 port);
755 
765  KInetSocketAddress& makeIPv4();
766 
775  KInetSocketAddress& makeIPv6();
776 
782  quint32 flowinfo() const;
783 
791  KInetSocketAddress& setFlowinfo(quint32 flowinfo);
792 
798  int scopeId() const;
799 
807  KInetSocketAddress& setScopeId(int scopeid);
808 
809 protected:
812  KInetSocketAddress(KSocketAddressData* d);
813 
814 private:
815  void update();
816 };
817 
818 /*
819  * External definition
820  */
821 
833 class KDECORE_EXPORT_DEPRECATED KUnixSocketAddress: public KSocketAddress
834 {
835  friend class KSocketAddress;
836 public:
840  KUnixSocketAddress();
841 
850  KUnixSocketAddress(const sockaddr* sa, quint16 len);
851 
858  KUnixSocketAddress(const KUnixSocketAddress& other);
859 
863  KUnixSocketAddress(const QString& pathname);
864 
868  virtual ~KUnixSocketAddress();
869 
876  KUnixSocketAddress& operator =(const KUnixSocketAddress& other);
877 
881  inline operator const sockaddr_un*() const
882  { return (const sockaddr_un*)address(); }
883 
888  QString pathname() const;
889 
895  KUnixSocketAddress& setPathname(const QString& path);
896 
897 protected:
900  KUnixSocketAddress(KSocketAddressData* d);
901 };
902 
903 } // namespace KNetwork
904 
905 #endif
KNetwork::KIpAddress::anyhostV6
static const KIpAddress anyhostV6
the any host or undefined address in IPv6 (::)
Definition: k3socketaddress.h:401
KNetwork::KUnixSocketAddress
A Unix (local) socket address.
Definition: k3socketaddress.h:833
KNetwork::KInetSocketAddress
an Internet socket address
Definition: k3socketaddress.h:643
KNetwork::KIpAddress::isSiteLocal
bool isSiteLocal() const
Returns true if this is an IPv6 site-local address.
Definition: k3socketaddress.h:321
KNetwork::KIpAddress
An IP address.
Definition: k3socketaddress.h:62
KNetwork::KIpAddress::isV4Compat
bool isV4Compat() const
Returns true if this is a v4-compat IPv6 address.
Definition: k3socketaddress.h:350
kdecore_export.h
sockaddr_in6
#define sockaddr_in6
Definition: netsupp.cpp:62
KNetwork::KIpAddress::isMulticastSiteLocal
bool isMulticastSiteLocal() const
Returns true if this is an IPv6 site-local multicast address.
Definition: k3socketaddress.h:372
KNetwork::KIpAddress::m_version
char m_version
Definition: k3socketaddress.h:390
KNetwork::KIpAddress::isMulticastNodeLocal
bool isMulticastNodeLocal() const
Returns true if this is an IPv6 node-local multicast address.
Definition: k3socketaddress.h:360
KNetwork::KIpAddress::isLoopback
bool isLoopback() const
This is an alias for isLocalhost().
Definition: k3socketaddress.h:258
quint32
KNetwork::KSocketAddress
A generic socket address.
Definition: k3socketaddress.h:414
QString
KNetwork::KSocketAddress::d
KSocketAddressData * d
Definition: k3socketaddress.h:609
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: k3socketaddress.h:276
KNetwork::KIpAddress::isUnspecified
bool isUnspecified() const
Returns true if this is the IPv4 or IPv6 unspecified address.
Definition: k3socketaddress.h:246
operator==
bool operator==(const KEntry &k1, const KEntry &k2)
Definition: kconfigdata.h:72
KNetwork::KIpAddress::localhostV4
static const KIpAddress localhostV4
localhost in IPv4 (127.0.0.1)
Definition: k3socketaddress.h:394
KNetwork::KIpAddress::isMulticastLinkLocal
bool isMulticastLinkLocal() const
Returns true if this is an IPv6 link-local multicast address.
Definition: k3socketaddress.h:366
KNetwork::KIpAddress::isLinkLocal
bool isLinkLocal() const
Returns true if this is an IPv6 link-local address.
Definition: k3socketaddress.h:310
KNetwork::KIpAddress::addr
const void * addr() const
Returns a pointer to binary raw data representing the address.
Definition: k3socketaddress.h:220
KNetwork::KIpAddress::isClassD
bool isClassD() const
Returns true if this is an IPv4 class D (a.k.a.
Definition: k3socketaddress.h:294
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: k3socketaddress.h:285
KNetwork::KIpAddress::KIpAddress
KIpAddress(quint32 ip4addr)
This is a convenience constructor.
Definition: k3socketaddress.h:122
KNetwork::KIpAddress::KIpAddress
KIpAddress()
Default constructor.
Definition: k3socketaddress.h:69
KNetwork::KIpAddress::anyhostV4
static const KIpAddress anyhostV4
the any host or undefined address in IPv4 (0.0.0.0)
Definition: k3socketaddress.h:396
KNetwork::KIpAddress::isIPv6Addr
bool isIPv6Addr() const
Returns true if this is an IPv6 address.
Definition: k3socketaddress.h:183
KNetwork::KSocketAddress::ianaFamily
int ianaFamily() const
Returns the IANA family number of this address.
Definition: k3socketaddress.h:541
KNetwork::KIpAddress::~KIpAddress
~KIpAddress()
Destructor.
Definition: k3socketaddress.h:131
KNetwork::KIpAddress::isMulticastOrgLocal
bool isMulticastOrgLocal() const
Returns true if this is an IPv6 organisational-local multicast address.
Definition: k3socketaddress.h:378
KNetwork::KIpAddress::isV4Mapped
bool isV4Mapped() const
Returns true if this is a v4-mapped IPv6 address.
Definition: k3socketaddress.h:338
KNetwork::KIpAddress::KIpAddress
KIpAddress(const void *addr, int version=4)
Creates an object from the given raw data and IP version.
Definition: k3socketaddress.h:109
KDE::version
unsigned int version()
Returns the encoded number of KDE's version, see the KDE_VERSION macro.
Definition: kdeversion.cpp:24
KNetwork::KIpAddress::KIpAddress
KIpAddress(const char *addr)
Creates an object from the given string representation.
Definition: k3socketaddress.h:100
KNetwork::KIpAddress::isMulticastGlobal
bool isMulticastGlobal() const
Returns true if this is an IPv6 global multicast address.
Definition: k3socketaddress.h:384
KNetwork::KIpAddress::IPv4Addr
quint32 IPv4Addr(bool convertMapped=true) const
This is a convenience function.
Definition: k3socketaddress.h:236
KNetwork::KIpAddress::version
int version() const
Retrieves the IP version in this object.
Definition: k3socketaddress.h:171
KNetwork::KIpAddress::isMulticast
bool isMulticast() const
Returns true if this is a multicast address, be it IPv4 or IPv6.
Definition: k3socketaddress.h:300
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: k3socketaddress.h:267
KNetwork::KIpAddress::KIpAddress
KIpAddress(const QString &addr)
Creates an object from the given string representation.
Definition: k3socketaddress.h:90
KNetwork::KIpAddress::isIPv4Addr
bool isIPv4Addr() const
Returns true if this is an IPv4 address.
Definition: k3socketaddress.h:177
KNetwork::KIpAddress::localhostV6
static const KIpAddress localhostV6
localhost in IPv6 (::1)
Definition: k3socketaddress.h:399
KNetwork::KIpAddress::isGlobal
bool isGlobal() const
Returns true if this is a global IPv6 address.
Definition: k3socketaddress.h:332
KNetwork::KIpAddress::isLocalhost
bool isLocalhost() const
Returns true if this is either the IPv4 or the IPv6 localhost address.
Definition: k3socketaddress.h:252
KNetwork::KIpAddress::KIpAddress
KIpAddress(const KIpAddress &other)
Copy constructor.
Definition: k3socketaddress.h:80
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal