fix logic
[personal-kdelibs.git] / kdecore / network / k3socketaddress.h
blobca7cae2b8c87bc806477c47eb1a01d41c5e73755
1 //krazy:excludeall=dpointer,inline (lightweight classes; kde3 support)
2 /* -*- C++ -*-
3 * Copyright (C) 2003,2005 Thiago Macieira <thiago@kde.org>
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 #ifndef KSOCKETADDRESS_H
27 #define KSOCKETADDRESS_H
29 #include <kdecore_export.h>
30 #include <QtCore/QByteArray>
32 struct sockaddr;
33 struct sockaddr_in;
34 struct sockaddr_in6;
35 struct sockaddr_un;
37 namespace KNetwork {
39 class KIpAddress;
40 class KSocketAddress;
41 class KInetSocketAddress;
42 class KUnixSocketAddress;
44 /** @class KIpAddress k3socketaddress.h k3socketaddress.h
45 * @brief An IP address.
47 * This class represents one IP address, version 4 or 6. This is only
48 * the address, not including port information or other data.
50 * It is not a good programming practice to create address from objects
51 * like this. Instead, prefer a more thorough function like
52 * KResolver::resolve(), which also handle extra information like scope
53 * ids.
55 * This is a light-weight class. Most of the member functions are inlined and
56 * there are no virtual functions. This object's size should be less than 20
57 * bytes. Also note that there is no sharing of data.
59 * @author Thiago Macieira <thiago@kde.org>
60 * @deprecated Use KSocketFactory or KLocalSocket instead
62 class KDECORE_EXPORT KIpAddress
64 public:
65 /**
66 * Default constructor. Creates an empty address.
67 * It defaults to IP version 4.
69 inline KIpAddress() : m_version(0)
70 { }
72 /**
73 * Copy constructor. Copies the data from the other
74 * object.
76 * Data is not shared.
78 * @param other the other
80 inline KIpAddress(const KIpAddress& other)
81 { *this = other; }
83 /**
84 * Creates an object from the given string representation.
86 * The IP version is guessed from the address format.
88 * @param addr the address
90 inline KIpAddress(const QString& addr)
91 { setAddress(addr); }
93 /**
94 * Creates an object from the given string representation.
96 * The IP version is guessed from the address format.
98 * @param addr the address
100 inline KIpAddress(const char* addr)
101 { setAddress(addr); }
104 * Creates an object from the given raw data and IP version.
106 * @param addr the raw data
107 * @param version the IP version (4 or 6)
109 inline KIpAddress(const void* addr, int version = 4)
110 { setAddress(addr, version); }
113 * This is a convenience constructor. Constructs an object
114 * from the given IPv4 address in the form of an integer.
116 * Note: do not write code to depend on IPv4 addresses being
117 * integer types. Instead, treat them as a special type, like
118 * a KIpAddress or the system's in_addr.
120 * @param ip4addr the IPv4 address
122 inline KIpAddress(quint32 ip4addr)
123 { setAddress(&ip4addr, 4); }
126 * Destructor. This frees resources associated with this object.
128 * Note: destructor is non-virtual. The compiler will happily optimise it
129 * out of the way.
131 inline ~KIpAddress()
135 * Copy operator.
137 * Copies the data from the other object into this one.
139 * @param other the object to copy
141 KIpAddress& operator =(const KIpAddress& other);
144 * Returns true if the two addresses match.
145 * This function performs a v4-mapped check.
146 * @see compare
148 inline bool operator ==(const KIpAddress& other) const
149 { return compare(other, true); }
152 * Compares this address against the other, supplied one and return
153 * true if they match. The @p checkMapped parameter controls whether
154 * a check for an IPv6 v4-mapped address will be performed.
156 * An IPv6 v4-mapped address is an IPv6 address that is, for all purposes,
157 * equivalent to an IPv4 one. The default behaviour of this function
158 * is to take that into account. If you want a strict matching,
159 * pass @b false to the @p checkMapped parameter.
161 * @param other the other IP address
162 * @param checkMapped whether v4-mapped addresses will be taken into account
164 bool compare(const KIpAddress& other, bool checkMapped = true) const;
167 * Retrieves the IP version in this object.
169 * @returns the version: 4 or 6
171 inline int version() const
172 { return m_version; }
175 * Returns true if this is an IPv4 address.
177 inline bool isIPv4Addr() const
178 { return version() == 4; }
181 * Returns true if this is an IPv6 address.
183 inline bool isIPv6Addr() const
184 { return version() == 6; }
187 * Sets the address to the given string representation.
189 * @return true if the address was successfully parsed; otherwise returns
190 * false and leaves the object unchanged.
192 bool setAddress(const QString& address);
195 * Sets the address to the given string representation.
197 * @return true if the address was successfully parsed; otherwise returns
198 * false and leaves the object unchanged.
200 bool setAddress(const char* address);
203 * Sets the address to the given raw binary representation.
205 * @param raw a pointer to the raw binary data
206 * @param version the IP version
207 * @return true if the address was successfully parsed; otherwise returns
208 * false and leaves the object unchanged.
210 bool setAddress(const void* raw, int version = 4);
213 * Returns the address as a string.
215 QString toString() const;
218 * Returns a pointer to binary raw data representing the address.
220 inline const void *addr() const
221 { return m_data; }
224 * This is a convenience function. Returns the IPv4 address in a
225 * 32-bit integer. The result is only valid if isIPv4Addr() returns
226 * true. Alternatively, if the contained IPv6 address is a v4-mapped one
227 * and the @p convertMapped parameter is true, the result will also be
228 * valid.
230 * Note: you should not treat IP addresses as integers. Instead,
231 * use types defined for that purpose, such as KIpAddress or the
232 * system's in_addr type.
234 * @bug Check if byte ordering is done right
236 inline quint32 IPv4Addr(bool convertMapped = true) const
238 return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0];
241 /*-- tests --*/
244 * Returns true if this is the IPv4 or IPv6 unspecified address.
246 inline bool isUnspecified() const
247 { return version() == 0 ? true : (*this == anyhostV4 || *this == anyhostV6); }
250 * Returns true if this is either the IPv4 or the IPv6 localhost address.
252 inline bool isLocalhost() const
253 { return version() == 0 ? false : (*this == localhostV4 || *this == localhostV6); }
256 * This is an alias for isLocalhost().
258 inline bool isLoopback() const
259 { return isLocalhost(); }
262 * Returns true if this is an IPv4 class A address, i.e.,
263 * from 0.0.0.0 to 127.255.255.255.
265 * This function does not test for v4-mapped addresses.
267 inline bool isClassA() const
268 { return version() != 4 ? false : (IPv4Addr() & 0x80000000) == 0; }
271 * Returns true if this is an IPv4 class B address, i.e., one from
272 * 128.0.0.0 to 191.255.255.255.
274 * This function does not test for v4-mapped addresses.
276 inline bool isClassB() const
277 { return version() != 4 ? false : (IPv4Addr() & 0xc0000000) == 0x80000000; }
280 * Returns true if this is an IPv4 class C address, i.e., one from
281 * 192.0.0.0 to 223.255.255.255.
283 * This function does not test for v4-mapped addresses.
285 inline bool isClassC() const
286 { return version() != 4 ? false : (IPv4Addr() & 0xe0000000) == 0xc0000000; }
289 * Returns true if this is an IPv4 class D (a.k.a. multicast) address.
291 * Note: this function is not the same as isMulticast(). isMulticast also
292 * tests for IPv6 multicast addresses.
294 inline bool isClassD() const
295 { return version() != 4 ? false : (IPv4Addr() & 0xf0000000) == 0xe0000000; }
298 * Returns true if this is a multicast address, be it IPv4 or IPv6.
300 inline bool isMulticast() const
302 if (version() == 4) return isClassD();
303 if (version() == 6) return ((quint8*)addr())[0] == 0xff;
304 return false;
308 * Returns true if this is an IPv6 link-local address.
310 inline bool isLinkLocal() const
312 if (version() != 6) return false;
313 quint8* addr = (quint8*)this->addr();
314 return (addr[0] & 0xff) == 0xfe &&
315 (addr[1] & 0xc0) == 0x80;
319 * Returns true if this is an IPv6 site-local address.
321 inline bool isSiteLocal() const
323 if (version() != 6) return false;
324 quint8* addr = (quint8*)this->addr();
325 return (addr[0] & 0xff) == 0xfe &&
326 (addr[1] & 0xc0) == 0xc0;
330 * Returns true if this is a global IPv6 address.
332 inline bool isGlobal() const
333 { return version() != 6 ? false : !(isMulticast() || isLinkLocal() || isSiteLocal()); }
336 * Returns true if this is a v4-mapped IPv6 address.
338 inline bool isV4Mapped() const
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;
348 * Returns true if this is a v4-compat IPv6 address.
350 inline bool isV4Compat() const
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;
358 * Returns true if this is an IPv6 node-local multicast address.
360 inline bool isMulticastNodeLocal() const
361 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x1; }
364 * Returns true if this is an IPv6 link-local multicast address.
366 inline bool isMulticastLinkLocal() const
367 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x2; }
370 * Returns true if this is an IPv6 site-local multicast address.
372 inline bool isMulticastSiteLocal() const
373 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x5; }
376 * Returns true if this is an IPv6 organisational-local multicast address.
378 inline bool isMulticastOrgLocal() const
379 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x8; }
382 * Returns true if this is an IPv6 global multicast address.
384 inline bool isMulticastGlobal() const
385 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0xe; }
387 protected:
388 quint32 m_data[4]; // 16 bytes, needed for an IPv6 address
390 char m_version;
392 public:
393 /// localhost in IPv4 (127.0.0.1)
394 static const KIpAddress localhostV4;
395 /// the any host or undefined address in IPv4 (0.0.0.0)
396 static const KIpAddress anyhostV4;
398 /// localhost in IPv6 (::1)
399 static const KIpAddress localhostV6;
400 /// the any host or undefined address in IPv6 (::)
401 static const KIpAddress anyhostV6;
405 class KSocketAddressData;
406 /** @class KSocketAddress k3socketaddress.h k3socketaddress.h
407 * @brief A generic socket address.
409 * This class holds one generic socket address.
411 * @author Thiago Macieira <thiago@kde.org>
412 * @deprecated Use KSocketFactory or KLocalSocket instead
414 class KDECORE_EXPORT KSocketAddress //krazy:exclude=dpointer (we got one, just not called Private)
416 public:
418 * Default constructor.
420 * Creates an empty object
422 KSocketAddress();
425 * Creates this object with the given data.
426 * The raw socket address is copied into this object.
428 * @param sa the socket address structure
429 * @param len the socket address length
431 KSocketAddress(const sockaddr* sa, quint16 len);
434 * Copy constructor. This creates a copy of the other
435 * object.
437 * Data is not shared.
439 * @param other the object to copy from
441 KSocketAddress(const KSocketAddress& other);
444 * Destructor. Frees any associated resources.
446 virtual ~KSocketAddress();
449 * Performs a shallow copy of the other object into this one.
450 * Data will be copied.
452 * @param other the object to copy from
454 KSocketAddress& operator =(const KSocketAddress& other);
457 * Returns the socket address structure, to be passed down to
458 * low level functions.
460 * Note that this function returns NULL for invalid or empty sockets,
461 * so you may use to to test for validity.
463 const sockaddr* address() const;
466 * Returns the socket address structure, to be passed down to
467 * low level functions.
469 * Note that this function returns NULL for invalid or empty sockets,
470 * so you may use to to test for validity.
472 * The returned value, if not NULL, is an internal buffer which is guaranteed
473 * to be at least length() bytes long.
475 sockaddr* address();
478 * Sets the address to the given address.
479 * The raw socket address is copied into this object.
481 * @param sa the socket address structure
482 * @param len the socket address length
484 KSocketAddress& setAddress(const sockaddr *sa, quint16 len);
487 * Returns the socket address structure, to be passed down to
488 * low level functions.
490 inline operator const sockaddr*() const
491 { return address(); }
494 * Returns the length of this socket address structure.
496 quint16 length() const;
499 * Sets the length of this socket structure.
501 * Use this function with care. It allows you to resize the internal
502 * buffer to fit needs. This function should not be used except for handling
503 * unknown socket address structures.
505 * Also note that this function may invalidate the socket if a known
506 * family is set (Internet or Unix socket) and the new length would be
507 * too small to hold the system's sockaddr_* structure. If unsure, reset
508 * the family:
510 * \code
511 * KSocketAddress qsa;
512 * [...]
513 * qsa.setFamily(AF_UNSPEC).setLength(newlen);
514 * \endcode
516 * @param len the new length
518 KSocketAddress& setLength(quint16 len);
521 * Returns the family of this address.
522 * @return the family of this address, AF_UNSPEC if it's undefined
524 int family() const;
527 * Sets the family of this object.
529 * Note: setting the family will probably invalidate any address data
530 * contained in this object. Use this function with care.
532 * @param family the new family to set
534 virtual KSocketAddress& setFamily(int family);
537 * Returns the IANA family number of this address.
538 * @return the IANA family number of this address (1 for AF_INET.
539 * 2 for AF_INET6, otherwise 0)
541 inline int ianaFamily() const
542 { return ianaFamily(family()); }
545 * Returns true if this equals the other socket.
547 * Socket addresses are considered matching if and only if all data is the same.
549 * @param other the other socket
550 * @return true if both sockets are equal
552 bool operator ==(const KSocketAddress& other) const;
555 * Returns the node name of this socket.
557 * In the case of Internet sockets, this is string representation of the IP address.
558 * The default implementation returns QString().
560 * @return the node name, can be QString()
561 * @bug use KResolver to resolve unknown families
563 virtual QString nodeName() const;
566 * Returns the service name for this socket.
568 * In the case of Internet sockets, this is the port number.
569 * The default implementation returns QString().
571 * @return the service name, can be QString()
572 * @bug use KResolver to resolve unknown families
574 virtual QString serviceName() const;
577 * Returns this socket address as a string suitable for
578 * printing. Family, node and service are part of this address.
580 * @bug use KResolver to resolve unknown families
582 virtual QString toString() const;
585 * Returns an object reference that can be used to manipulate this socket
586 * as an Internet socket address. Both objects share the same data.
588 KInetSocketAddress& asInet();
591 * Returns an object is equal to this object's data, but they don't share it.
593 KInetSocketAddress asInet() const;
596 * Returns an object reference that can be used to manipulate this socket
597 * as a Unix socket address. Both objects share the same data.
599 KUnixSocketAddress& asUnix();
602 * Returns an object is equal to this object's data, but they don't share it.
604 KUnixSocketAddress asUnix() const;
606 protected:
607 /// @internal
608 /// private data
609 KSocketAddressData *d;
611 /// @internal
612 /// extra constructor
613 KSocketAddress(KSocketAddressData* d);
615 public: // static
617 * Returns the IANA family number of the given address family.
618 * Returns 0 if there is no corresponding IANA family number.
619 * @param af the address family, in AF_* constants
620 * @return the IANA family number of this address (1 for AF_INET.
621 * 2 for AF_INET6, otherwise 0)
623 static int ianaFamily(int af);
626 * Returns the address family of the given IANA family number.
627 * @return the address family, AF_UNSPEC for unknown IANA family numbers
629 static int fromIanaFamily(int iana);
633 /** @class KInetSocketAddress k3socketaddress.h k3socketaddress.h
634 * @brief an Internet socket address
636 * An Inet (IPv4 or IPv6) socket address
638 * This is an IPv4 or IPv6 address of the Internet.
640 * @author Thiago Macieira <thiago@kde.org>
641 * @deprecated Use KSocketFactory or KLocalSocket instead
643 class KDECORE_EXPORT KInetSocketAddress: public KSocketAddress
645 friend class KSocketAddress;
646 public:
648 * Public constructor. Creates an empty object.
650 KInetSocketAddress();
653 * Creates an object from raw data.
655 * Note: if the socket address @p sa does not contain a valid Internet
656 * socket (IPv4 or IPv6), this object will be empty.
658 * @param sa the sockaddr structure
659 * @param len the structure's length
661 KInetSocketAddress(const sockaddr* sa, quint16 len);
664 * Creates an object from an IP address and port.
666 * @param host the IP address
667 * @param port the port number
669 KInetSocketAddress(const KIpAddress& host, quint16 port);
672 * Copy constructor.
674 * Data is not shared.
676 * @param other the other object
678 KInetSocketAddress(const KInetSocketAddress& other);
681 * Copy constructor.
683 * If the other, generic socket address contains an Internet address,
684 * it will be copied. Otherwise, this object will be empty.
686 * @param other the other object
688 KInetSocketAddress(const KSocketAddress& other);
691 * Destroys this object.
693 virtual ~KInetSocketAddress();
696 * Copy operator.
698 * Copies the other object into this one.
700 * @param other the other object
702 KInetSocketAddress& operator =(const KInetSocketAddress& other);
705 * Cast operator to sockaddr_in.
707 inline operator const sockaddr_in*() const
708 { return (const sockaddr_in*)address(); }
711 * Cast operator to sockaddr_in6.
713 inline operator const sockaddr_in6*() const
714 { return (const sockaddr_in6*)address(); }
717 * Returns the IP version of the address this object holds.
719 * @return 4 or 6, if IPv4 or IPv6, respectively; 0 if this object is empty
721 int ipVersion() const;
724 * Returns the IP address component.
726 KIpAddress ipAddress() const;
729 * Sets the IP address to the given raw address.
731 * This call will preserve port numbers across IP versions, but will lose
732 * IPv6 specific data if the address is set to IPv4.
734 * @param addr the address to set to
735 * @return a reference to itself
737 KInetSocketAddress& setHost(const KIpAddress& addr);
740 * Retrieves the port number stored in this object.
742 * @return a port number in the range 0 to 65535, inclusive. An empty or
743 * invalid object will have a port number of 0.
745 quint16 port() const;
748 * Sets the port number. If this object is empty, this function will default to
749 * creating an IPv4 address.
751 * @param port the port number to set
752 * @return a reference to itself
754 KInetSocketAddress& setPort(quint16 port);
757 * Converts this object to an IPv4 socket address. It has no effect if the object
758 * is already an IPv4 socket address.
760 * If this object is an IPv6 address, the port number is preserved. All other information
761 * is lost.
763 * @return a reference to itself
765 KInetSocketAddress& makeIPv4();
768 * Converts this object to an IPv6 socket address. It has no effect if the object
769 * is already an IPv6 socket address.
771 * If this object is an IPv4 address, the port number is preserved.
773 * @return a reference to itself
775 KInetSocketAddress& makeIPv6();
778 * Returns the flowinfo information from the IPv6 socket address.
780 * @return the flowinfo information or 0 if this object is empty or IPv4
782 quint32 flowinfo() const;
785 * Sets the flowinfo information for an IPv6 socket address. If this is not
786 * an IPv6 socket address, this function converts it to one. See makeIPv6.
788 * @param flowinfo the flowinfo to set
789 * @return a reference to itself
791 KInetSocketAddress& setFlowinfo(quint32 flowinfo);
794 * Returns the scope id this IPv6 socket is bound to.
796 * @return the scope id, or 0 if this is not an IPv6 object
798 int scopeId() const;
801 * Sets the scope id for this IPv6 object. If this is not an IPv6 socket
802 * address, this function converts it to one. See makeIPv6
804 * @param scopeid the scopeid to set
805 * @return a reference to itself
807 KInetSocketAddress& setScopeId(int scopeid);
809 protected:
810 /// @internal
811 /// extra constructor
812 KInetSocketAddress(KSocketAddressData* d);
814 private:
815 void update();
819 * External definition
822 /** @class KUnixSocketAddress k3socketaddress.h k3socketaddress.h
823 * @brief A Unix (local) socket address.
825 * This is a Unix socket address.
827 * Note that this class uses QStrings to represent filenames, which means
828 * the proper encoding is used to translate into valid filesystem file names.
830 * @author Thiago Macieira <thiago@kde.org>
831 * @deprecated Use KSocketFactory or KLocalSocket instead
833 class KDECORE_EXPORT KUnixSocketAddress: public KSocketAddress
835 friend class KSocketAddress;
836 public:
838 * Default constructor. Creates an empty object.
840 KUnixSocketAddress();
843 * Creates this object with the given raw data. If
844 * the sockaddr structure does not contain a Local namespace
845 * (Unix) socket, this object will be created empty.
847 * @param sa the socket address structure
848 * @param len the structure's length
850 KUnixSocketAddress(const sockaddr* sa, quint16 len);
853 * Copy constructor. Creates a copy of the other object,
854 * sharing the data explicitly.
856 * @param other the other object
858 KUnixSocketAddress(const KUnixSocketAddress& other);
861 * Constructs an object from the given pathname.
863 KUnixSocketAddress(const QString& pathname);
866 * Destructor.
868 virtual ~KUnixSocketAddress();
871 * Copy operator. Copies the contents of the other object into
872 * this one. Data is explicitly shared.
874 * @param other the other
876 KUnixSocketAddress& operator =(const KUnixSocketAddress& other);
879 * Cast operator to sockaddr_un.
881 inline operator const sockaddr_un*() const
882 { return (const sockaddr_un*)address(); }
885 * Returns the pathname associated with this object. Will return
886 * QString() if this object is empty.
888 QString pathname() const;
891 * Sets the pathname for the object.
893 * @return a reference to itself
895 KUnixSocketAddress& setPathname(const QString& path);
897 protected:
898 /// @internal
899 /// extra constructor
900 KUnixSocketAddress(KSocketAddressData* d);
903 } // namespace KNetwork
905 #endif