KDELibs4Support

k3socketaddress.h
1 //krazy:excludeall=dpointer,inline (lightweight classes; kde3 support)
2 /* -*- C++ -*-
3  * Copyright (C) 2003,2005 Thiago Macieira <[email protected]>
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 <kdelibs4support_export.h>
30 #include <QByteArray>
31 
32 struct sockaddr;
33 struct sockaddr_in;
34 struct sockaddr_in6;
35 struct sockaddr_un;
36 
37 namespace KNetwork
38 {
39 
40 class KIpAddress;
41 class KSocketAddress;
42 class KInetSocketAddress;
43 class KUnixSocketAddress;
44 
63 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KIpAddress
64 {
65 public:
70  inline KIpAddress() : m_version(0)
71  { }
72 
81  inline KIpAddress(const KIpAddress &other)
82  {
83  *this = other;
84  }
85 
93  inline KIpAddress(const QString &addr)
94  {
95  setAddress(addr);
96  }
97 
105  inline KIpAddress(const char *addr)
106  {
107  setAddress(addr);
108  }
109 
116  inline KIpAddress(const void *addr, int version = 4)
117  {
118  setAddress(addr, version);
119  }
120 
131  inline KIpAddress(quint32 ip4addr)
132  {
133  setAddress(&ip4addr, 4);
134  }
135 
142  inline ~KIpAddress()
143  { }
144 
152  KIpAddress &operator =(const KIpAddress &other);
153 
159  inline bool operator ==(const KIpAddress &other) const
160  {
161  return compare(other, true);
162  }
163 
177  bool compare(const KIpAddress &other, bool checkMapped = true) const;
178 
184  inline int version() const
185  {
186  return m_version;
187  }
188 
192  inline bool isIPv4Addr() const
193  {
194  return version() == 4;
195  }
196 
200  inline bool isIPv6Addr() const
201  {
202  return version() == 6;
203  }
204 
211  bool setAddress(const QString &address);
212 
219  bool setAddress(const char *address);
220 
229  bool setAddress(const void *raw, int version = 4);
230 
234  QString toString() const;
235 
239  inline const void *addr() const
240  {
241  return m_data;
242  }
243 
257  inline quint32 IPv4Addr(bool convertMapped = true) const
258  {
259  return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0];
260  }
261 
262  /*-- tests --*/
263 
267  inline bool isUnspecified() const
268  {
269  return version() == 0 ? true : (*this == anyhostV4 || *this == anyhostV6);
270  }
271 
275  inline bool isLocalhost() const
276  {
277  return version() == 0 ? false : (*this == localhostV4 || *this == localhostV6);
278  }
279 
283  inline bool isLoopback() const
284  {
285  return isLocalhost();
286  }
287 
294  inline bool isClassA() const
295  {
296  return version() != 4 ? false : (IPv4Addr() & 0x80000000) == 0;
297  }
298 
305  inline bool isClassB() const
306  {
307  return version() != 4 ? false : (IPv4Addr() & 0xc0000000) == 0x80000000;
308  }
309 
316  inline bool isClassC() const
317  {
318  return version() != 4 ? false : (IPv4Addr() & 0xe0000000) == 0xc0000000;
319  }
320 
327  inline bool isClassD() const
328  {
329  return version() != 4 ? false : (IPv4Addr() & 0xf0000000) == 0xe0000000;
330  }
331 
335  inline bool isMulticast() const
336  {
337  if (version() == 4) {
338  return isClassD();
339  }
340  if (version() == 6) {
341  return ((quint8 *)addr())[0] == 0xff;
342  }
343  return false;
344  }
345 
349  inline bool isLinkLocal() const
350  {
351  if (version() != 6) {
352  return false;
353  }
354  quint8 *addr = (quint8 *)this->addr();
355  return (addr[0] & 0xff) == 0xfe &&
356  (addr[1] & 0xc0) == 0x80;
357  }
358 
362  inline bool isSiteLocal() const
363  {
364  if (version() != 6) {
365  return false;
366  }
367  quint8 *addr = (quint8 *)this->addr();
368  return (addr[0] & 0xff) == 0xfe &&
369  (addr[1] & 0xc0) == 0xc0;
370  }
371 
375  inline bool isGlobal() const
376  {
377  return version() != 6 ? false : !(isMulticast() || isLinkLocal() || isSiteLocal());
378  }
379 
383  inline bool isV4Mapped() const
384  {
385  if (version() != 6) {
386  return false;
387  }
388  quint32 *addr = (quint32 *)this->addr();
389  return addr[0] == 0 && addr[1] == 0 &&
390  ((quint16 *)&addr[2])[0] == 0 &&
391  ((quint16 *)&addr[2])[1] == 0xffff;
392  }
393 
397  inline bool isV4Compat() const
398  {
399  if (version() != 6 || isLocalhost()) {
400  return false;
401  }
402  quint32 *addr = (quint32 *)this->addr();
403  return addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] != 0;
404  }
405 
409  inline bool isMulticastNodeLocal() const
410  {
411  return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0x1;
412  }
413 
417  inline bool isMulticastLinkLocal() const
418  {
419  return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0x2;
420  }
421 
425  inline bool isMulticastSiteLocal() const
426  {
427  return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0x5;
428  }
429 
433  inline bool isMulticastOrgLocal() const
434  {
435  return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0x8;
436  }
437 
441  inline bool isMulticastGlobal() const
442  {
443  return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0xe;
444  }
445 
446 protected:
447  quint32 m_data[4]; // 16 bytes, needed for an IPv6 address
448 
449  char m_version;
450 
451 public:
453  static const KIpAddress localhostV4;
455  static const KIpAddress anyhostV4;
456 
458  static const KIpAddress localhostV6;
460  static const KIpAddress anyhostV6;
461 };
462 
463 class KSocketAddressData;
472 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KSocketAddress //krazy:exclude=dpointer (we got one, just not called Private)
473 {
474 public:
480  KSocketAddress();
481 
489  KSocketAddress(const sockaddr *sa, quint16 len);
490 
499  KSocketAddress(const KSocketAddress &other);
500 
504  virtual ~KSocketAddress();
505 
512  KSocketAddress &operator =(const KSocketAddress &other);
513 
521  const sockaddr *address() const;
522 
533  sockaddr *address();
534 
542  KSocketAddress &setAddress(const sockaddr *sa, quint16 len);
543 
548  inline operator const sockaddr *() const
549  {
550  return address();
551  }
552 
556  quint16 length() const;
557 
578  KSocketAddress &setLength(quint16 len);
579 
584  int family() const;
585 
594  virtual KSocketAddress &setFamily(int family);
595 
601  inline int ianaFamily() const
602  {
603  return ianaFamily(family());
604  }
605 
614  bool operator ==(const KSocketAddress &other) const;
615 
625  virtual QString nodeName() const;
626 
636  virtual QString serviceName() const;
637 
644  virtual QString toString() const;
645 
650  KInetSocketAddress &asInet();
651 
655  KInetSocketAddress asInet() const;
656 
661  KUnixSocketAddress &asUnix();
662 
666  KUnixSocketAddress asUnix() const;
667 
668 protected:
671  KSocketAddressData *d;
672 
675  KSocketAddress(KSocketAddressData *d);
676 
677 public: // static
685  static int ianaFamily(int af);
686 
691  static int fromIanaFamily(int iana);
692 };
693 
704 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KInetSocketAddress: public KSocketAddress
705 {
706  friend class KSocketAddress;
707 public:
712 
722  KInetSocketAddress(const sockaddr *sa, quint16 len);
723 
730  KInetSocketAddress(const KIpAddress &host, quint16 port);
731 
740 
749  KInetSocketAddress(const KSocketAddress &other);
750 
754  virtual ~KInetSocketAddress();
755 
763  KInetSocketAddress &operator =(const KInetSocketAddress &other);
764 
768  inline operator const sockaddr_in *() const
769  {
770  return (const sockaddr_in *)address();
771  }
772 
776  inline operator const sockaddr_in6 *() const
777  {
778  return (const sockaddr_in6 *)address();
779  }
780 
786  int ipVersion() const;
787 
791  KIpAddress ipAddress() const;
792 
802  KInetSocketAddress &setHost(const KIpAddress &addr);
803 
810  quint16 port() const;
811 
819  KInetSocketAddress &setPort(quint16 port);
820 
830  KInetSocketAddress &makeIPv4();
831 
840  KInetSocketAddress &makeIPv6();
841 
847  quint32 flowinfo() const;
848 
856  KInetSocketAddress &setFlowinfo(quint32 flowinfo);
857 
863  int scopeId() const;
864 
872  KInetSocketAddress &setScopeId(int scopeid);
873 
874 protected:
877  KInetSocketAddress(KSocketAddressData *d);
878 
879 private:
880  void update();
881 };
882 
883 /*
884  * External definition
885  */
886 
898 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KUnixSocketAddress: public KSocketAddress
899 {
900  friend class KSocketAddress;
901 public:
906 
915  KUnixSocketAddress(const sockaddr *sa, quint16 len);
916 
924 
928  KUnixSocketAddress(const QString &pathname);
929 
933  virtual ~KUnixSocketAddress();
934 
941  KUnixSocketAddress &operator =(const KUnixSocketAddress &other);
942 
946  inline operator const sockaddr_un *() const
947  {
948  return (const sockaddr_un *)address();
949  }
950 
955  QString pathname() const;
956 
962  KUnixSocketAddress &setPathname(const QString &path);
963 
964 protected:
967  KUnixSocketAddress(KSocketAddressData *d);
968 };
969 
970 } // namespace KNetwork
971 
972 #endif
static const KIpAddress anyhostV6
the any host or undefined address in IPv6 (::)
A Unix (local) socket address.
an Internet socket address
bool isSiteLocal() const
Returns true if this is an IPv6 site-local address.
An IP address.
bool isV4Compat() const
Returns true if this is a v4-compat IPv6 address.
bool isMulticastSiteLocal() const
Returns true if this is an IPv6 site-local multicast address.
bool isMulticastNodeLocal() const
Returns true if this is an IPv6 node-local multicast address.
bool isLoopback() const
This is an alias for isLocalhost().
A generic socket address.
KSocketAddressData * d
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.
bool isUnspecified() const
Returns true if this is the IPv4 or IPv6 unspecified address.
A namespace to store all networking-related (socket) classes.
static const KIpAddress localhostV4
localhost in IPv4 (127.0.0.1)
bool isMulticastLinkLocal() const
Returns true if this is an IPv6 link-local multicast address.
bool isLinkLocal() const
Returns true if this is an IPv6 link-local address.
const void * addr() const
Returns a pointer to binary raw data representing the address.
bool isClassD() const
Returns true if this is an IPv4 class D (a.k.a.
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.
KIpAddress(quint32 ip4addr)
This is a convenience constructor.
KIpAddress()
Default constructor.
static const KIpAddress anyhostV4
the any host or undefined address in IPv4 (0.0.0.0)
bool isIPv6Addr() const
Returns true if this is an IPv6 address.
int ianaFamily() const
Returns the IANA family number of this address.
~KIpAddress()
Destructor.
bool isMulticastOrgLocal() const
Returns true if this is an IPv6 organisational-local multicast address.
bool isV4Mapped() const
Returns true if this is a v4-mapped IPv6 address.
KIpAddress(const void *addr, int version=4)
Creates an object from the given raw data and IP version.
unsigned int version()
Returns the encoded number of KDE&#39;s version, see the KDE_VERSION macro.
Definition: kdeversion.cpp:24
KIpAddress(const char *addr)
Creates an object from the given string representation.
bool isMulticastGlobal() const
Returns true if this is an IPv6 global multicast address.
quint32 IPv4Addr(bool convertMapped=true) const
This is a convenience function.
int version() const
Retrieves the IP version in this object.
bool isMulticast() const
Returns true if this is a multicast address, be it IPv4 or IPv6.
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.
KIpAddress(const QString &addr)
Creates an object from the given string representation.
bool isIPv4Addr() const
Returns true if this is an IPv4 address.
static const KIpAddress localhostV6
localhost in IPv6 (::1)
bool isGlobal() const
Returns true if this is a global IPv6 address.
bool isLocalhost() const
Returns true if this is either the IPv4 or the IPv6 localhost address.
KIpAddress(const KIpAddress &other)
Copy constructor.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Jul 5 2020 22:58:43 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.