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 
45 /** @class KIpAddress k3socketaddress.h k3socketaddress.h
46  * @brief An IP address.
47  *
48  * This class represents one IP address, version 4 or 6. This is only
49  * the address, not including port information or other data.
50  *
51  * It is not a good programming practice to create address from objects
52  * like this. Instead, prefer a more thorough function like
53  * KResolver::resolve(), which also handle extra information like scope
54  * ids.
55  *
56  * This is a light-weight class. Most of the member functions are inlined and
57  * there are no virtual functions. This object's size should be less than 20
58  * bytes. Also note that there is no sharing of data.
59  *
60  * @author Thiago Macieira <[email protected]>
61  * @deprecated Use KSocketFactory or KLocalSocket instead
62  */
63 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KIpAddress
64 {
65 public:
66  /**
67  * Default constructor. Creates an empty address.
68  * It defaults to IP version 4.
69  */
70  inline KIpAddress() : m_version(0)
71  { }
72 
73  /**
74  * Copy constructor. Copies the data from the other
75  * object.
76  *
77  * Data is not shared.
78  *
79  * @param other the other
80  */
81  inline KIpAddress(const KIpAddress &other)
82  {
83  *this = other;
84  }
85 
86  /**
87  * Creates an object from the given string representation.
88  *
89  * The IP version is guessed from the address format.
90  *
91  * @param addr the address
92  */
93  inline KIpAddress(const QString &addr)
94  {
95  setAddress(addr);
96  }
97 
98  /**
99  * Creates an object from the given string representation.
100  *
101  * The IP version is guessed from the address format.
102  *
103  * @param addr the address
104  */
105  inline KIpAddress(const char *addr)
106  {
107  setAddress(addr);
108  }
109 
110  /**
111  * Creates an object from the given raw data and IP version.
112  *
113  * @param addr the raw data
114  * @param version the IP version (4 or 6)
115  */
116  inline KIpAddress(const void *addr, int version = 4)
117  {
118  setAddress(addr, version);
119  }
120 
121  /**
122  * This is a convenience constructor. Constructs an object
123  * from the given IPv4 address in the form of an integer.
124  *
125  * Note: do not write code to depend on IPv4 addresses being
126  * integer types. Instead, treat them as a special type, like
127  * a KIpAddress or the system's in_addr.
128  *
129  * @param ip4addr the IPv4 address
130  */
131  inline KIpAddress(quint32 ip4addr)
132  {
133  setAddress(&ip4addr, 4);
134  }
135 
136  /**
137  * Destructor. This frees resources associated with this object.
138  *
139  * Note: destructor is non-virtual. The compiler will happily optimize it
140  * out of the way.
141  */
142  inline ~KIpAddress()
143  { }
144 
145  /**
146  * Copy operator.
147  *
148  * Copies the data from the other object into this one.
149  *
150  * @param other the object to copy
151  */
152  KIpAddress &operator =(const KIpAddress &other);
153 
154  /**
155  * Returns true if the two addresses match.
156  * This function performs a v4-mapped check.
157  * @see compare
158  */
159  inline bool operator ==(const KIpAddress &other) const
160  {
161  return compare(other, true);
162  }
163 
164  /**
165  * Compares this address against the other, supplied one and return
166  * true if they match. The @p checkMapped parameter controls whether
167  * a check for an IPv6 v4-mapped address will be performed.
168  *
169  * An IPv6 v4-mapped address is an IPv6 address that is, for all purposes,
170  * equivalent to an IPv4 one. The default behaviour of this function
171  * is to take that into account. If you want a strict matching,
172  * pass @b false to the @p checkMapped parameter.
173  *
174  * @param other the other IP address
175  * @param checkMapped whether v4-mapped addresses will be taken into account
176  */
177  bool compare(const KIpAddress &other, bool checkMapped = true) const;
178 
179  /**
180  * Retrieves the IP version in this object.
181  *
182  * @returns the version: 4 or 6
183  */
184  inline int version() const
185  {
186  return m_version;
187  }
188 
189  /**
190  * Returns true if this is an IPv4 address.
191  */
192  inline bool isIPv4Addr() const
193  {
194  return version() == 4;
195  }
196 
197  /**
198  * Returns true if this is an IPv6 address.
199  */
200  inline bool isIPv6Addr() const
201  {
202  return version() == 6;
203  }
204 
205  /**
206  * Sets the address to the given string representation.
207  *
208  * @return true if the address was successfully parsed; otherwise returns
209  * false and leaves the object unchanged.
210  */
211  bool setAddress(const QString &address);
212 
213  /**
214  * Sets the address to the given string representation.
215  *
216  * @return true if the address was successfully parsed; otherwise returns
217  * false and leaves the object unchanged.
218  */
219  bool setAddress(const char *address);
220 
221  /**
222  * Sets the address to the given raw binary representation.
223  *
224  * @param raw a pointer to the raw binary data
225  * @param version the IP version
226  * @return true if the address was successfully parsed; otherwise returns
227  * false and leaves the object unchanged.
228  */
229  bool setAddress(const void *raw, int version = 4);
230 
231  /**
232  * Returns the address as a string.
233  */
234  QString toString() const;
235 
236  /**
237  * Returns a pointer to binary raw data representing the address.
238  */
239  inline const void *addr() const
240  {
241  return m_data;
242  }
243 
244  /**
245  * This is a convenience function. Returns the IPv4 address in a
246  * 32-bit integer. The result is only valid if isIPv4Addr() returns
247  * true. Alternatively, if the contained IPv6 address is a v4-mapped one
248  * and the @p convertMapped parameter is true, the result will also be
249  * valid.
250  *
251  * Note: you should not treat IP addresses as integers. Instead,
252  * use types defined for that purpose, such as KIpAddress or the
253  * system's in_addr type.
254  *
255  * @bug Check if byte ordering is done right
256  */
257  inline quint32 IPv4Addr(bool convertMapped = true) const
258  {
259  return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0];
260  }
261 
262  /*-- tests --*/
263 
264  /**
265  * Returns true if this is the IPv4 or IPv6 unspecified address.
266  */
267  inline bool isUnspecified() const
268  {
269  return version() == 0 ? true : (*this == anyhostV4 || *this == anyhostV6);
270  }
271 
272  /**
273  * Returns true if this is either the IPv4 or the IPv6 localhost address.
274  */
275  inline bool isLocalhost() const
276  {
277  return version() == 0 ? false : (*this == localhostV4 || *this == localhostV6);
278  }
279 
280  /**
281  * This is an alias for isLocalhost().
282  */
283  inline bool isLoopback() const
284  {
285  return isLocalhost();
286  }
287 
288  /**
289  * Returns true if this is an IPv4 class A address, i.e.,
290  * from 0.0.0.0 to 127.255.255.255.
291  *
292  * This function does not test for v4-mapped addresses.
293  */
294  inline bool isClassA() const
295  {
296  return version() != 4 ? false : (IPv4Addr() & 0x80000000) == 0;
297  }
298 
299  /**
300  * Returns true if this is an IPv4 class B address, i.e., one from
301  * 128.0.0.0 to 191.255.255.255.
302  *
303  * This function does not test for v4-mapped addresses.
304  */
305  inline bool isClassB() const
306  {
307  return version() != 4 ? false : (IPv4Addr() & 0xc0000000) == 0x80000000;
308  }
309 
310  /**
311  * Returns true if this is an IPv4 class C address, i.e., one from
312  * 192.0.0.0 to 223.255.255.255.
313  *
314  * This function does not test for v4-mapped addresses.
315  */
316  inline bool isClassC() const
317  {
318  return version() != 4 ? false : (IPv4Addr() & 0xe0000000) == 0xc0000000;
319  }
320 
321  /**
322  * Returns true if this is an IPv4 class D (a.k.a. multicast) address.
323  *
324  * Note: this function is not the same as isMulticast(). isMulticast also
325  * tests for IPv6 multicast addresses.
326  */
327  inline bool isClassD() const
328  {
329  return version() != 4 ? false : (IPv4Addr() & 0xf0000000) == 0xe0000000;
330  }
331 
332  /**
333  * Returns true if this is a multicast address, be it IPv4 or IPv6.
334  */
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 
346  /**
347  * Returns true if this is an IPv6 link-local address.
348  */
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 
359  /**
360  * Returns true if this is an IPv6 site-local address.
361  */
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 
372  /**
373  * Returns true if this is a global IPv6 address.
374  */
375  inline bool isGlobal() const
376  {
377  return version() != 6 ? false : !(isMulticast() || isLinkLocal() || isSiteLocal());
378  }
379 
380  /**
381  * Returns true if this is a v4-mapped IPv6 address.
382  */
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 
394  /**
395  * Returns true if this is a v4-compat IPv6 address.
396  */
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 
406  /**
407  * Returns true if this is an IPv6 node-local multicast address.
408  */
409  inline bool isMulticastNodeLocal() const
410  {
411  return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0x1;
412  }
413 
414  /**
415  * Returns true if this is an IPv6 link-local multicast address.
416  */
417  inline bool isMulticastLinkLocal() const
418  {
419  return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0x2;
420  }
421 
422  /**
423  * Returns true if this is an IPv6 site-local multicast address.
424  */
425  inline bool isMulticastSiteLocal() const
426  {
427  return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0x5;
428  }
429 
430  /**
431  * Returns true if this is an IPv6 organisational-local multicast address.
432  */
433  inline bool isMulticastOrgLocal() const
434  {
435  return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0x8;
436  }
437 
438  /**
439  * Returns true if this is an IPv6 global multicast address.
440  */
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:
452  /// localhost in IPv4 (127.0.0.1)
453  static const KIpAddress localhostV4;
454  /// the any host or undefined address in IPv4 (0.0.0.0)
455  static const KIpAddress anyhostV4;
456 
457  /// localhost in IPv6 (::1)
458  static const KIpAddress localhostV6;
459  /// the any host or undefined address in IPv6 (::)
460  static const KIpAddress anyhostV6;
461 };
462 
463 class KSocketAddressData;
464 /** @class KSocketAddress k3socketaddress.h k3socketaddress.h
465  * @brief A generic socket address.
466  *
467  * This class holds one generic socket address.
468  *
469  * @author Thiago Macieira <[email protected]>
470  * @deprecated Use KSocketFactory or KLocalSocket instead
471  */
472 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KSocketAddress //krazy:exclude=dpointer (we got one, just not called Private)
473 {
474 public:
475  /**
476  * Default constructor.
477  *
478  * Creates an empty object
479  */
480  KSocketAddress();
481 
482  /**
483  * Creates this object with the given data.
484  * The raw socket address is copied into this object.
485  *
486  * @param sa the socket address structure
487  * @param len the socket address length
488  */
489  KSocketAddress(const sockaddr *sa, quint16 len);
490 
491  /**
492  * Copy constructor. This creates a copy of the other
493  * object.
494  *
495  * Data is not shared.
496  *
497  * @param other the object to copy from
498  */
499  KSocketAddress(const KSocketAddress &other);
500 
501  /**
502  * Destructor. Frees any associated resources.
503  */
504  virtual ~KSocketAddress();
505 
506  /**
507  * Performs a shallow copy of the other object into this one.
508  * Data will be copied.
509  *
510  * @param other the object to copy from
511  */
512  KSocketAddress &operator =(const KSocketAddress &other);
513 
514  /**
515  * Returns the socket address structure, to be passed down to
516  * low level functions.
517  *
518  * Note that this function returns NULL for invalid or empty sockets,
519  * so you may use to to test for validity.
520  */
521  const sockaddr *address() const;
522 
523  /**
524  * Returns the socket address structure, to be passed down to
525  * low level functions.
526  *
527  * Note that this function returns NULL for invalid or empty sockets,
528  * so you may use to to test for validity.
529  *
530  * The returned value, if not NULL, is an internal buffer which is guaranteed
531  * to be at least length() bytes long.
532  */
533  sockaddr *address();
534 
535  /**
536  * Sets the address to the given address.
537  * The raw socket address is copied into this object.
538  *
539  * @param sa the socket address structure
540  * @param len the socket address length
541  */
542  KSocketAddress &setAddress(const sockaddr *sa, quint16 len);
543 
544  /**
545  * Returns the socket address structure, to be passed down to
546  * low level functions.
547  */
548  inline operator const sockaddr *() const
549  {
550  return address();
551  }
552 
553  /**
554  * Returns the length of this socket address structure.
555  */
556  quint16 length() const;
557 
558  /**
559  * Sets the length of this socket structure.
560  *
561  * Use this function with care. It allows you to resize the internal
562  * buffer to fit needs. This function should not be used except for handling
563  * unknown socket address structures.
564  *
565  * Also note that this function may invalidate the socket if a known
566  * family is set (Internet or Unix socket) and the new length would be
567  * too small to hold the system's sockaddr_* structure. If unsure, reset
568  * the family:
569  *
570  * \code
571  * KSocketAddress qsa;
572  * [...]
573  * qsa.setFamily(AF_UNSPEC).setLength(newlen);
574  * \endcode
575  *
576  * @param len the new length
577  */
578  KSocketAddress &setLength(quint16 len);
579 
580  /**
581  * Returns the family of this address.
582  * @return the family of this address, AF_UNSPEC if it's undefined
583  */
584  int family() const;
585 
586  /**
587  * Sets the family of this object.
588  *
589  * Note: setting the family will probably invalidate any address data
590  * contained in this object. Use this function with care.
591  *
592  * @param family the new family to set
593  */
594  virtual KSocketAddress &setFamily(int family);
595 
596  /**
597  * Returns the IANA family number of this address.
598  * @return the IANA family number of this address (1 for AF_INET.
599  * 2 for AF_INET6, otherwise 0)
600  */
601  inline int ianaFamily() const
602  {
603  return ianaFamily(family());
604  }
605 
606  /**
607  * Returns true if this equals the other socket.
608  *
609  * Socket addresses are considered matching if and only if all data is the same.
610  *
611  * @param other the other socket
612  * @return true if both sockets are equal
613  */
614  bool operator ==(const KSocketAddress &other) const;
615 
616  /**
617  * Returns the node name of this socket.
618  *
619  * In the case of Internet sockets, this is string representation of the IP address.
620  * The default implementation returns QString().
621  *
622  * @return the node name, can be QString()
623  * @bug use KResolver to resolve unknown families
624  */
625  virtual QString nodeName() const;
626 
627  /**
628  * Returns the service name for this socket.
629  *
630  * In the case of Internet sockets, this is the port number.
631  * The default implementation returns QString().
632  *
633  * @return the service name, can be QString()
634  * @bug use KResolver to resolve unknown families
635  */
636  virtual QString serviceName() const;
637 
638  /**
639  * Returns this socket address as a string suitable for
640  * printing. Family, node and service are part of this address.
641  *
642  * @bug use KResolver to resolve unknown families
643  */
644  virtual QString toString() const;
645 
646  /**
647  * Returns an object reference that can be used to manipulate this socket
648  * as an Internet socket address. Both objects share the same data.
649  */
650  KInetSocketAddress &asInet();
651 
652  /**
653  * Returns an object is equal to this object's data, but they don't share it.
654  */
655  KInetSocketAddress asInet() const;
656 
657  /**
658  * Returns an object reference that can be used to manipulate this socket
659  * as a Unix socket address. Both objects share the same data.
660  */
661  KUnixSocketAddress &asUnix();
662 
663  /**
664  * Returns an object is equal to this object's data, but they don't share it.
665  */
666  KUnixSocketAddress asUnix() const;
667 
668 protected:
669  /// @internal
670  /// private data
671  KSocketAddressData *d;
672 
673  /// @internal
674  /// extra constructor
675  KSocketAddress(KSocketAddressData *d);
676 
677 public: // static
678  /**
679  * Returns the IANA family number of the given address family.
680  * Returns 0 if there is no corresponding IANA family number.
681  * @param af the address family, in AF_* constants
682  * @return the IANA family number of this address (1 for AF_INET.
683  * 2 for AF_INET6, otherwise 0)
684  */
685  static int ianaFamily(int af);
686 
687  /**
688  * Returns the address family of the given IANA family number.
689  * @return the address family, AF_UNSPEC for unknown IANA family numbers
690  */
691  static int fromIanaFamily(int iana);
692 };
693 
694 /** @class KInetSocketAddress k3socketaddress.h k3socketaddress.h
695  * @brief an Internet socket address
696  *
697  * An Inet (IPv4 or IPv6) socket address
698  *
699  * This is an IPv4 or IPv6 address of the Internet.
700  *
701  * @author Thiago Macieira <[email protected]>
702  * @deprecated Use KSocketFactory or KLocalSocket instead
703  */
704 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KInetSocketAddress: public KSocketAddress
705 {
706  friend class KSocketAddress;
707 public:
708  /**
709  * Public constructor. Creates an empty object.
710  */
712 
713  /**
714  * Creates an object from raw data.
715  *
716  * Note: if the socket address @p sa does not contain a valid Internet
717  * socket (IPv4 or IPv6), this object will be empty.
718  *
719  * @param sa the sockaddr structure
720  * @param len the structure's length
721  */
722  KInetSocketAddress(const sockaddr *sa, quint16 len);
723 
724  /**
725  * Creates an object from an IP address and port.
726  *
727  * @param host the IP address
728  * @param port the port number
729  */
730  KInetSocketAddress(const KIpAddress &host, quint16 port);
731 
732  /**
733  * Copy constructor.
734  *
735  * Data is not shared.
736  *
737  * @param other the other object
738  */
740 
741  /**
742  * Copy constructor.
743  *
744  * If the other, generic socket address contains an Internet address,
745  * it will be copied. Otherwise, this object will be empty.
746  *
747  * @param other the other object
748  */
749  KInetSocketAddress(const KSocketAddress &other);
750 
751  /**
752  * Destroys this object.
753  */
754  ~KInetSocketAddress() override;
755 
756  /**
757  * Copy operator.
758  *
759  * Copies the other object into this one.
760  *
761  * @param other the other object
762  */
763  KInetSocketAddress &operator =(const KInetSocketAddress &other);
764 
765  /**
766  * Cast operator to sockaddr_in.
767  */
768  inline operator const sockaddr_in *() const
769  {
770  return (const sockaddr_in *)address();
771  }
772 
773  /**
774  * Cast operator to sockaddr_in6.
775  */
776  inline operator const sockaddr_in6 *() const
777  {
778  return (const sockaddr_in6 *)address();
779  }
780 
781  /**
782  * Returns the IP version of the address this object holds.
783  *
784  * @return 4 or 6, if IPv4 or IPv6, respectively; 0 if this object is empty
785  */
786  int ipVersion() const;
787 
788  /**
789  * Returns the IP address component.
790  */
791  KIpAddress ipAddress() const;
792 
793  /**
794  * Sets the IP address to the given raw address.
795  *
796  * This call will preserve port numbers across IP versions, but will lose
797  * IPv6 specific data if the address is set to IPv4.
798  *
799  * @param addr the address to set to
800  * @return a reference to itself
801  */
802  KInetSocketAddress &setHost(const KIpAddress &addr);
803 
804  /**
805  * Retrieves the port number stored in this object.
806  *
807  * @return a port number in the range 0 to 65535, inclusive. An empty or
808  * invalid object will have a port number of 0.
809  */
810  quint16 port() const;
811 
812  /**
813  * Sets the port number. If this object is empty, this function will default to
814  * creating an IPv4 address.
815  *
816  * @param port the port number to set
817  * @return a reference to itself
818  */
819  KInetSocketAddress &setPort(quint16 port);
820 
821  /**
822  * Converts this object to an IPv4 socket address. It has no effect if the object
823  * is already an IPv4 socket address.
824  *
825  * If this object is an IPv6 address, the port number is preserved. All other information
826  * is lost.
827  *
828  * @return a reference to itself
829  */
830  KInetSocketAddress &makeIPv4();
831 
832  /**
833  * Converts this object to an IPv6 socket address. It has no effect if the object
834  * is already an IPv6 socket address.
835  *
836  * If this object is an IPv4 address, the port number is preserved.
837  *
838  * @return a reference to itself
839  */
840  KInetSocketAddress &makeIPv6();
841 
842  /**
843  * Returns the flowinfo information from the IPv6 socket address.
844  *
845  * @return the flowinfo information or 0 if this object is empty or IPv4
846  */
847  quint32 flowinfo() const;
848 
849  /**
850  * Sets the flowinfo information for an IPv6 socket address. If this is not
851  * an IPv6 socket address, this function converts it to one. See makeIPv6.
852  *
853  * @param flowinfo the flowinfo to set
854  * @return a reference to itself
855  */
856  KInetSocketAddress &setFlowinfo(quint32 flowinfo);
857 
858  /**
859  * Returns the scope id this IPv6 socket is bound to.
860  *
861  * @return the scope id, or 0 if this is not an IPv6 object
862  */
863  int scopeId() const;
864 
865  /**
866  * Sets the scope id for this IPv6 object. If this is not an IPv6 socket
867  * address, this function converts it to one. See makeIPv6
868  *
869  * @param scopeid the scopeid to set
870  * @return a reference to itself
871  */
872  KInetSocketAddress &setScopeId(int scopeid);
873 
874 protected:
875  /// @internal
876  /// extra constructor
877  KInetSocketAddress(KSocketAddressData *d);
878 
879 private:
880  void update();
881 };
882 
883 /*
884  * External definition
885  */
886 
887 /** @class KUnixSocketAddress k3socketaddress.h k3socketaddress.h
888  * @brief A Unix (local) socket address.
889  *
890  * This is a Unix socket address.
891  *
892  * Note that this class uses QStrings to represent filenames, which means
893  * the proper encoding is used to translate into valid filesystem file names.
894  *
895  * @author Thiago Macieira <[email protected]>
896  * @deprecated Use KSocketFactory or KLocalSocket instead
897  */
898 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KUnixSocketAddress: public KSocketAddress
899 {
900  friend class KSocketAddress;
901 public:
902  /**
903  * Default constructor. Creates an empty object.
904  */
906 
907  /**
908  * Creates this object with the given raw data. If
909  * the sockaddr structure does not contain a Local namespace
910  * (Unix) socket, this object will be created empty.
911  *
912  * @param sa the socket address structure
913  * @param len the structure's length
914  */
915  KUnixSocketAddress(const sockaddr *sa, quint16 len);
916 
917  /**
918  * Copy constructor. Creates a copy of the other object,
919  * sharing the data explicitly.
920  *
921  * @param other the other object
922  */
924 
925  /**
926  * Constructs an object from the given pathname.
927  */
928  KUnixSocketAddress(const QString &pathname);
929 
930  /**
931  * Destructor.
932  */
933  ~KUnixSocketAddress() override;
934 
935  /**
936  * Copy operator. Copies the contents of the other object into
937  * this one. Data is explicitly shared.
938  *
939  * @param other the other
940  */
941  KUnixSocketAddress &operator =(const KUnixSocketAddress &other);
942 
943  /**
944  * Cast operator to sockaddr_un.
945  */
946  inline operator const sockaddr_un *() const
947  {
948  return (const sockaddr_un *)address();
949  }
950 
951  /**
952  * Returns the pathname associated with this object. Will return
953  * QString() if this object is empty.
954  */
955  QString pathname() const;
956 
957  /**
958  * Sets the pathname for the object.
959  *
960  * @return a reference to itself
961  */
962  KUnixSocketAddress &setPathname(const QString &path);
963 
964 protected:
965  /// @internal
966  /// extra constructor
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-2021 The KDE developers.
Generated on Tue Mar 2 2021 23:56:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.