• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
  • network
tdesocketaddress.h
1/*
2 * Copyright (C) 2003 Thiago Macieira <thiago.macieira@kdemail.net>
3 *
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef TDESOCKETADDRESS_H
26#define TDESOCKETADDRESS_H
27
28#include <tqstring.h>
29#include <tqcstring.h>
30
31#include <tdelibs_export.h>
32
33struct sockaddr;
34struct sockaddr_in;
35struct sockaddr_in6;
36struct sockaddr_un;
37
38namespace KNetwork {
39
40class KIpAddress;
41class TDESocketAddress;
42class KInetSocketAddress;
43class KUnixSocketAddress;
44
62class TDECORE_EXPORT KIpAddress
63{
64public:
69 inline KIpAddress() : m_version(0)
70 { }
71
80 inline KIpAddress(const KIpAddress& other)
81 { *this = other; }
82
90 inline KIpAddress(const TQString& 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(TQ_UINT32 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 TQString& address);
193
200 bool setAddress(const char* address);
201
210 bool setAddress(const void* raw, int version = 4);
211
215 TQString toString() const;
216
220 inline const void *addr() const
221 { return m_data; }
222
235 inline TQ_UINT32 IPv4Addr(bool convertMapped = true) const
236 {
237 return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0];
238 }
239
248 TQ_UINT32 hostIPv4Addr(bool convertMapped = true) const;
249
250public:
251 /*-- tests --*/
252
256 inline bool isUnspecified() const
257 { return version() == 0 ? true : (*this == anyhostV4 || *this == anyhostV6); }
258
262 inline bool isLocalhost() const
263 { return version() == 0 ? false : (*this == localhostV4 || *this == localhostV6); }
264
268 inline bool isLoopback() const
269 { return isLocalhost(); }
270
277 inline bool isClassA() const
278 { return version() != 4 ? false : (hostIPv4Addr() & 0x80000000) == 0; }
279
286 inline bool isClassB() const
287 { return version() != 4 ? false : (hostIPv4Addr() & 0xc0000000) == 0x80000000; }
288
295 inline bool isClassC() const
296 { return version() != 4 ? false : (hostIPv4Addr() & 0xe0000000) == 0xc0000000; }
297
304 inline bool isClassD() const
305 { return version() != 4 ? false : (hostIPv4Addr() & 0xf0000000) == 0xe0000000; }
306
310 inline bool isMulticast() const
311 {
312 if (version() == 4) return isClassD();
313 if (version() == 6) return ((TQ_UINT8*)addr())[0] == 0xff;
314 return false;
315 }
316
320 inline bool isLinkLocal() const
321 {
322 if (version() != 6) return false;
323 TQ_UINT8* addr = (TQ_UINT8*)this->addr();
324 return (addr[0] & 0xff) == 0xfe &&
325 (addr[1] & 0xc0) == 0x80;
326 }
327
331 inline bool isSiteLocal() const
332 {
333 if (version() != 6) return false;
334 TQ_UINT8* addr = (TQ_UINT8*)this->addr();
335 return (addr[0] & 0xff) == 0xfe &&
336 (addr[1] & 0xc0) == 0xc0;
337 }
338
342 inline bool isGlobal() const
343 { return version() != 6 ? false : !(isMulticast() || isLinkLocal() || isSiteLocal()); }
344
348 inline bool isV4Mapped() const
349 {
350 if (version() != 6) return false;
351 TQ_UINT32* addr = (TQ_UINT32*)this->addr();
352 return addr[0] == 0 && addr[1] == 0 &&
353 ((TQ_UINT16*)&addr[2])[0] == 0 &&
354 ((TQ_UINT16*)&addr[2])[1] == 0xffff;
355 }
356
360 inline bool isV4Compat() const
361 {
362 if (version() != 6 || isLocalhost()) return false;
363 TQ_UINT32* addr = (TQ_UINT32*)this->addr();
364 return addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] != 0;
365 }
366
370 inline bool isMulticastNodeLocal() const
371 { return version() == 6 && isMulticast() && (((TQ_UINT32*)addr())[0] & 0xf) == 0x1; }
372
376 inline bool isMulticastLinkLocal() const
377 { return version() == 6 && isMulticast() && (((TQ_UINT32*)addr())[0] & 0xf) == 0x2; }
378
382 inline bool isMulticastSiteLocal() const
383 { return version() == 6 && isMulticast() && (((TQ_UINT32*)addr())[0] & 0xf) == 0x5; }
384
388 inline bool isMulticastOrgLocal() const
389 { return version() == 6 && isMulticast() && (((TQ_UINT32*)addr())[0] & 0xf) == 0x8; }
390
394 inline bool isMulticastGlobal() const
395 { return version() == 6 && isMulticast() && (((TQ_UINT32*)addr())[0] & 0xf) == 0xe; }
396
397protected:
398 TQ_UINT32 m_data[4]; // 16 bytes, needed for an IPv6 address
399
400 char m_version;
401
402public:
404 static const KIpAddress localhostV4;
406 static const KIpAddress anyhostV4;
407
409 static const KIpAddress localhostV6;
411 static const KIpAddress anyhostV6;
412};
413
414
415class TDESocketAddressData;
423class TDECORE_EXPORT TDESocketAddress
424{
425public:
431 TDESocketAddress();
432
440 TDESocketAddress(const sockaddr* sa, TQ_UINT16 len);
441
450 TDESocketAddress(const TDESocketAddress& other);
451
455 virtual ~TDESocketAddress();
456
463 TDESocketAddress& operator =(const TDESocketAddress& other);
464
472 const sockaddr* address() const;
473
484 sockaddr* address();
485
493 TDESocketAddress& setAddress(const sockaddr *sa, TQ_UINT16 len);
494
499 inline operator const sockaddr*() const
500 { return address(); }
501
505 TQ_UINT16 length() const;
506
527 TDESocketAddress& setLength(TQ_UINT16 len);
528
533 int family() const;
534
543 virtual TDESocketAddress& setFamily(int family);
544
550 inline int ianaFamily() const
551 { return ianaFamily(family()); }
552
561 bool operator ==(const TDESocketAddress& other) const;
562
572 virtual TQString nodeName() const;
573
583 virtual TQString serviceName() const;
584
591 virtual TQString toString() const;
592
597 KInetSocketAddress& asInet();
598
602 KInetSocketAddress asInet() const;
603
608 KUnixSocketAddress& asUnix();
609
613 KUnixSocketAddress asUnix() const;
614
615protected:
618 TDESocketAddressData *d;
619
622 TDESocketAddress(TDESocketAddressData* d);
623
624public: // static
632 static int ianaFamily(int af);
633
638 static int fromIanaFamily(int iana);
639};
640
641
651class TDECORE_EXPORT KInetSocketAddress: public TDESocketAddress
652{
653 friend class TDESocketAddress;
654public:
658 KInetSocketAddress();
659
669 KInetSocketAddress(const sockaddr* sa, TQ_UINT16 len);
670
677 KInetSocketAddress(const KIpAddress& host, TQ_UINT16 port);
678
686 KInetSocketAddress(const KInetSocketAddress& other);
687
696 KInetSocketAddress(const TDESocketAddress& other);
697
701 virtual ~KInetSocketAddress();
702
710 KInetSocketAddress& operator =(const KInetSocketAddress& other);
711
715 inline operator const sockaddr_in*() const
716 { return (const sockaddr_in*)address(); }
717
721 inline operator const sockaddr_in6*() const
722 { return (const sockaddr_in6*)address(); }
723
729 int ipVersion() const;
730
734 KIpAddress ipAddress() const;
735
745 KInetSocketAddress& setHost(const KIpAddress& addr);
746
753 TQ_UINT16 port() const;
754
762 KInetSocketAddress& setPort(TQ_UINT16 port);
763
773 KInetSocketAddress& makeIPv4();
774
783 KInetSocketAddress& makeIPv6();
784
790 TQ_UINT32 flowinfo() const;
791
799 KInetSocketAddress& setFlowinfo(TQ_UINT32 flowinfo);
800
806 int scopeId() const;
807
815 KInetSocketAddress& setScopeId(int scopeid);
816
817protected:
820 KInetSocketAddress(TDESocketAddressData* d);
821
822private:
823 void update();
824};
825
826/*
827 * External definition
828 */
829
840class TDECORE_EXPORT KUnixSocketAddress: public TDESocketAddress
841{
842 friend class TDESocketAddress;
843public:
847 KUnixSocketAddress();
848
857 KUnixSocketAddress(const sockaddr* sa, TQ_UINT16 len);
858
865 KUnixSocketAddress(const KUnixSocketAddress& other);
866
870 KUnixSocketAddress(const TQString& pathname);
871
875 virtual ~KUnixSocketAddress();
876
883 KUnixSocketAddress& operator =(const KUnixSocketAddress& other);
884
888 inline operator const sockaddr_un*() const
889 { return (const sockaddr_un*)address(); }
890
895 TQString pathname() const;
896
902 KUnixSocketAddress& setPathname(const TQString& path);
903
904protected:
907 KUnixSocketAddress(TDESocketAddressData* d);
908};
909
910} // namespace KNetwork
911
912#endif
KInetSocketAddress
An Inet (IPv4 or IPv6) socket address.
Definition: ksockaddr.h:234
KNetwork::KInetSocketAddress
an Internet socket address
Definition: tdesocketaddress.h:652
KNetwork::KIpAddress
An IP address.
Definition: tdesocketaddress.h:63
KNetwork::KIpAddress::isSiteLocal
bool isSiteLocal() const
Returns true if this is an IPv6 site-local address.
Definition: tdesocketaddress.h:331
KNetwork::KIpAddress::KIpAddress
KIpAddress(TQ_UINT32 ip4addr)
This is a convenience constructor.
Definition: tdesocketaddress.h:122
KNetwork::KIpAddress::isMulticastOrgLocal
bool isMulticastOrgLocal() const
Returns true if this is an IPv6 organisational-local multicast address.
Definition: tdesocketaddress.h:388
KNetwork::KIpAddress::version
int version() const
Retrieves the IP version in this object.
Definition: tdesocketaddress.h:171
KNetwork::KIpAddress::KIpAddress
KIpAddress(const void *addr, int version=4)
Creates an object from the given raw data and IP version.
Definition: tdesocketaddress.h:109
KNetwork::KIpAddress::KIpAddress
KIpAddress(const KIpAddress &other)
Copy constructor.
Definition: tdesocketaddress.h:80
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....
Definition: tdesocketaddress.h:295
KNetwork::KIpAddress::isIPv6Addr
bool isIPv6Addr() const
Returns true if this is an IPv6 address.
Definition: tdesocketaddress.h:183
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....
Definition: tdesocketaddress.h:286
KNetwork::KIpAddress::KIpAddress
KIpAddress()
Default constructor.
Definition: tdesocketaddress.h:69
KNetwork::KIpAddress::isLocalhost
bool isLocalhost() const
Returns true if this is either the IPv4 or the IPv6 localhost address.
Definition: tdesocketaddress.h:262
KNetwork::KIpAddress::IPv4Addr
TQ_UINT32 IPv4Addr(bool convertMapped=true) const
This is a convenience function.
Definition: tdesocketaddress.h:235
KNetwork::KIpAddress::addr
const void * addr() const
Returns a pointer to binary raw data representing the address.
Definition: tdesocketaddress.h:220
KNetwork::KIpAddress::localhostV4
static const KIpAddress localhostV4
localhost in IPv4 (127.0.0.1)
Definition: tdesocketaddress.h:404
KNetwork::KIpAddress::isLinkLocal
bool isLinkLocal() const
Returns true if this is an IPv6 link-local address.
Definition: tdesocketaddress.h:320
KNetwork::KIpAddress::KIpAddress
KIpAddress(const TQString &addr)
Creates an object from the given string representation.
Definition: tdesocketaddress.h:90
KNetwork::KIpAddress::isMulticastSiteLocal
bool isMulticastSiteLocal() const
Returns true if this is an IPv6 site-local multicast address.
Definition: tdesocketaddress.h:382
KNetwork::KIpAddress::isMulticast
bool isMulticast() const
Returns true if this is a multicast address, be it IPv4 or IPv6.
Definition: tdesocketaddress.h:310
KNetwork::KIpAddress::isMulticastNodeLocal
bool isMulticastNodeLocal() const
Returns true if this is an IPv6 node-local multicast address.
Definition: tdesocketaddress.h:370
KNetwork::KIpAddress::isUnspecified
bool isUnspecified() const
Returns true if this is the IPv4 or IPv6 unspecified address.
Definition: tdesocketaddress.h:256
KNetwork::KIpAddress::isMulticastLinkLocal
bool isMulticastLinkLocal() const
Returns true if this is an IPv6 link-local multicast address.
Definition: tdesocketaddress.h:376
KNetwork::KIpAddress::isV4Compat
bool isV4Compat() const
Returns true if this is a v4-compat IPv6 address.
Definition: tdesocketaddress.h:360
KNetwork::KIpAddress::isClassD
bool isClassD() const
Returns true if this is an IPv4 class D (a.k.a.
Definition: tdesocketaddress.h:304
KNetwork::KIpAddress::isGlobal
bool isGlobal() const
Returns true if this is a global IPv6 address.
Definition: tdesocketaddress.h:342
KNetwork::KIpAddress::anyhostV6
static const KIpAddress anyhostV6
the any host or undefined address in IPv6 (::)
Definition: tdesocketaddress.h:411
KNetwork::KIpAddress::isLoopback
bool isLoopback() const
This is an alias for isLocalhost.
Definition: tdesocketaddress.h:268
KNetwork::KIpAddress::localhostV6
static const KIpAddress localhostV6
localhost in IPv6 (::1)
Definition: tdesocketaddress.h:409
KNetwork::KIpAddress::~KIpAddress
~KIpAddress()
Destructor.
Definition: tdesocketaddress.h:131
KNetwork::KIpAddress::anyhostV4
static const KIpAddress anyhostV4
the any host or undefined address in IPv4 (0.0.0.0)
Definition: tdesocketaddress.h:406
KNetwork::KIpAddress::isMulticastGlobal
bool isMulticastGlobal() const
Returns true if this is an IPv6 global multicast address.
Definition: tdesocketaddress.h:394
KNetwork::KIpAddress::isV4Mapped
bool isV4Mapped() const
Returns true if this is a v4-mapped IPv6 address.
Definition: tdesocketaddress.h:348
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....
Definition: tdesocketaddress.h:277
KNetwork::KIpAddress::isIPv4Addr
bool isIPv4Addr() const
Returns true if this is an IPv4 address.
Definition: tdesocketaddress.h:177
KNetwork::KIpAddress::KIpAddress
KIpAddress(const char *addr)
Creates an object from the given string representation.
Definition: tdesocketaddress.h:100
KNetwork::KUnixSocketAddress
A Unix (local) socket address.
Definition: tdesocketaddress.h:841
KNetwork::TDESocketAddress
A generic socket address.
Definition: tdesocketaddress.h:424
KNetwork::TDESocketAddress::ianaFamily
int ianaFamily() const
Returns the IANA family number of this address.
Definition: tdesocketaddress.h:550
KUnixSocketAddress
A Unix socket address.
Definition: ksockaddr.h:585
TDESocketAddress
A socket address.
Definition: ksockaddr.h:47
TDESocketAddress::address
const sockaddr * address() const
Returns a sockaddr structure, for passing down to library functions.
Definition: ksockaddr.h:78
KNetwork
A namespace to store all networking-related (socket) classes.
Definition: kbufferedsocket.h:36

tdecore

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

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.