File indexing completed on 2024-09-01 13:22:08
0001 //krazy:excludeall=dpointer,inline (lightweight classes; kde3 support) 0002 /* -*- C++ -*- 0003 * Copyright (C) 2003,2005 Thiago Macieira <thiago@kde.org> 0004 * 0005 * 0006 * Permission is hereby granted, free of charge, to any person obtaining 0007 * a copy of this software and associated documentation files (the 0008 * "Software"), to deal in the Software without restriction, including 0009 * without limitation the rights to use, copy, modify, merge, publish, 0010 * distribute, sublicense, and/or sell copies of the Software, and to 0011 * permit persons to whom the Software is furnished to do so, subject to 0012 * the following conditions: 0013 * 0014 * The above copyright notice and this permission notice shall be included 0015 * in all copies or substantial portions of the Software. 0016 * 0017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 0018 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 0019 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 0020 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 0021 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 0022 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 0023 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 0024 */ 0025 0026 #ifndef KSOCKETADDRESS_H 0027 #define KSOCKETADDRESS_H 0028 0029 #include <kdelibs4support_export.h> 0030 #include <QByteArray> 0031 0032 struct sockaddr; 0033 struct sockaddr_in; 0034 struct sockaddr_in6; 0035 struct sockaddr_un; 0036 0037 namespace KNetwork 0038 { 0039 0040 class KIpAddress; 0041 class KSocketAddress; 0042 class KInetSocketAddress; 0043 class KUnixSocketAddress; 0044 0045 /** @class KIpAddress k3socketaddress.h k3socketaddress.h 0046 * @brief An IP address. 0047 * 0048 * This class represents one IP address, version 4 or 6. This is only 0049 * the address, not including port information or other data. 0050 * 0051 * It is not a good programming practice to create address from objects 0052 * like this. Instead, prefer a more thorough function like 0053 * KResolver::resolve(), which also handle extra information like scope 0054 * ids. 0055 * 0056 * This is a light-weight class. Most of the member functions are inlined and 0057 * there are no virtual functions. This object's size should be less than 20 0058 * bytes. Also note that there is no sharing of data. 0059 * 0060 * @author Thiago Macieira <thiago@kde.org> 0061 * @deprecated Use KSocketFactory or KLocalSocket instead 0062 */ 0063 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KIpAddress 0064 { 0065 public: 0066 /** 0067 * Default constructor. Creates an empty address. 0068 * It defaults to IP version 4. 0069 */ 0070 inline KIpAddress() : m_version(0) 0071 { } 0072 0073 /** 0074 * Copy constructor. Copies the data from the other 0075 * object. 0076 * 0077 * Data is not shared. 0078 * 0079 * @param other the other 0080 */ 0081 inline KIpAddress(const KIpAddress &other) 0082 { 0083 *this = other; 0084 } 0085 0086 /** 0087 * Creates an object from the given string representation. 0088 * 0089 * The IP version is guessed from the address format. 0090 * 0091 * @param addr the address 0092 */ 0093 inline KIpAddress(const QString &addr) 0094 { 0095 setAddress(addr); 0096 } 0097 0098 /** 0099 * Creates an object from the given string representation. 0100 * 0101 * The IP version is guessed from the address format. 0102 * 0103 * @param addr the address 0104 */ 0105 inline KIpAddress(const char *addr) 0106 { 0107 setAddress(addr); 0108 } 0109 0110 /** 0111 * Creates an object from the given raw data and IP version. 0112 * 0113 * @param addr the raw data 0114 * @param version the IP version (4 or 6) 0115 */ 0116 inline KIpAddress(const void *addr, int version = 4) 0117 { 0118 setAddress(addr, version); 0119 } 0120 0121 /** 0122 * This is a convenience constructor. Constructs an object 0123 * from the given IPv4 address in the form of an integer. 0124 * 0125 * Note: do not write code to depend on IPv4 addresses being 0126 * integer types. Instead, treat them as a special type, like 0127 * a KIpAddress or the system's in_addr. 0128 * 0129 * @param ip4addr the IPv4 address 0130 */ 0131 inline KIpAddress(quint32 ip4addr) 0132 { 0133 setAddress(&ip4addr, 4); 0134 } 0135 0136 /** 0137 * Destructor. This frees resources associated with this object. 0138 * 0139 * Note: destructor is non-virtual. The compiler will happily optimize it 0140 * out of the way. 0141 */ 0142 inline ~KIpAddress() 0143 { } 0144 0145 /** 0146 * Copy operator. 0147 * 0148 * Copies the data from the other object into this one. 0149 * 0150 * @param other the object to copy 0151 */ 0152 KIpAddress &operator =(const KIpAddress &other); 0153 0154 /** 0155 * Returns true if the two addresses match. 0156 * This function performs a v4-mapped check. 0157 * @see compare 0158 */ 0159 inline bool operator ==(const KIpAddress &other) const 0160 { 0161 return compare(other, true); 0162 } 0163 0164 /** 0165 * Compares this address against the other, supplied one and return 0166 * true if they match. The @p checkMapped parameter controls whether 0167 * a check for an IPv6 v4-mapped address will be performed. 0168 * 0169 * An IPv6 v4-mapped address is an IPv6 address that is, for all purposes, 0170 * equivalent to an IPv4 one. The default behaviour of this function 0171 * is to take that into account. If you want a strict matching, 0172 * pass @b false to the @p checkMapped parameter. 0173 * 0174 * @param other the other IP address 0175 * @param checkMapped whether v4-mapped addresses will be taken into account 0176 */ 0177 bool compare(const KIpAddress &other, bool checkMapped = true) const; 0178 0179 /** 0180 * Retrieves the IP version in this object. 0181 * 0182 * @returns the version: 4 or 6 0183 */ 0184 inline int version() const 0185 { 0186 return m_version; 0187 } 0188 0189 /** 0190 * Returns true if this is an IPv4 address. 0191 */ 0192 inline bool isIPv4Addr() const 0193 { 0194 return version() == 4; 0195 } 0196 0197 /** 0198 * Returns true if this is an IPv6 address. 0199 */ 0200 inline bool isIPv6Addr() const 0201 { 0202 return version() == 6; 0203 } 0204 0205 /** 0206 * Sets the address to the given string representation. 0207 * 0208 * @return true if the address was successfully parsed; otherwise returns 0209 * false and leaves the object unchanged. 0210 */ 0211 bool setAddress(const QString &address); 0212 0213 /** 0214 * Sets the address to the given string representation. 0215 * 0216 * @return true if the address was successfully parsed; otherwise returns 0217 * false and leaves the object unchanged. 0218 */ 0219 bool setAddress(const char *address); 0220 0221 /** 0222 * Sets the address to the given raw binary representation. 0223 * 0224 * @param raw a pointer to the raw binary data 0225 * @param version the IP version 0226 * @return true if the address was successfully parsed; otherwise returns 0227 * false and leaves the object unchanged. 0228 */ 0229 bool setAddress(const void *raw, int version = 4); 0230 0231 /** 0232 * Returns the address as a string. 0233 */ 0234 QString toString() const; 0235 0236 /** 0237 * Returns a pointer to binary raw data representing the address. 0238 */ 0239 inline const void *addr() const 0240 { 0241 return m_data; 0242 } 0243 0244 /** 0245 * This is a convenience function. Returns the IPv4 address in a 0246 * 32-bit integer. The result is only valid if isIPv4Addr() returns 0247 * true. Alternatively, if the contained IPv6 address is a v4-mapped one 0248 * and the @p convertMapped parameter is true, the result will also be 0249 * valid. 0250 * 0251 * Note: you should not treat IP addresses as integers. Instead, 0252 * use types defined for that purpose, such as KIpAddress or the 0253 * system's in_addr type. 0254 * 0255 * @bug Check if byte ordering is done right 0256 */ 0257 inline quint32 IPv4Addr(bool convertMapped = true) const 0258 { 0259 return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0]; 0260 } 0261 0262 /*-- tests --*/ 0263 0264 /** 0265 * Returns true if this is the IPv4 or IPv6 unspecified address. 0266 */ 0267 inline bool isUnspecified() const 0268 { 0269 return version() == 0 ? true : (*this == anyhostV4 || *this == anyhostV6); 0270 } 0271 0272 /** 0273 * Returns true if this is either the IPv4 or the IPv6 localhost address. 0274 */ 0275 inline bool isLocalhost() const 0276 { 0277 return version() == 0 ? false : (*this == localhostV4 || *this == localhostV6); 0278 } 0279 0280 /** 0281 * This is an alias for isLocalhost(). 0282 */ 0283 inline bool isLoopback() const 0284 { 0285 return isLocalhost(); 0286 } 0287 0288 /** 0289 * Returns true if this is an IPv4 class A address, i.e., 0290 * from 0.0.0.0 to 127.255.255.255. 0291 * 0292 * This function does not test for v4-mapped addresses. 0293 */ 0294 inline bool isClassA() const 0295 { 0296 return version() != 4 ? false : (IPv4Addr() & 0x80000000) == 0; 0297 } 0298 0299 /** 0300 * Returns true if this is an IPv4 class B address, i.e., one from 0301 * 128.0.0.0 to 191.255.255.255. 0302 * 0303 * This function does not test for v4-mapped addresses. 0304 */ 0305 inline bool isClassB() const 0306 { 0307 return version() != 4 ? false : (IPv4Addr() & 0xc0000000) == 0x80000000; 0308 } 0309 0310 /** 0311 * Returns true if this is an IPv4 class C address, i.e., one from 0312 * 192.0.0.0 to 223.255.255.255. 0313 * 0314 * This function does not test for v4-mapped addresses. 0315 */ 0316 inline bool isClassC() const 0317 { 0318 return version() != 4 ? false : (IPv4Addr() & 0xe0000000) == 0xc0000000; 0319 } 0320 0321 /** 0322 * Returns true if this is an IPv4 class D (a.k.a. multicast) address. 0323 * 0324 * Note: this function is not the same as isMulticast(). isMulticast also 0325 * tests for IPv6 multicast addresses. 0326 */ 0327 inline bool isClassD() const 0328 { 0329 return version() != 4 ? false : (IPv4Addr() & 0xf0000000) == 0xe0000000; 0330 } 0331 0332 /** 0333 * Returns true if this is a multicast address, be it IPv4 or IPv6. 0334 */ 0335 inline bool isMulticast() const 0336 { 0337 if (version() == 4) { 0338 return isClassD(); 0339 } 0340 if (version() == 6) { 0341 return ((quint8 *)addr())[0] == 0xff; 0342 } 0343 return false; 0344 } 0345 0346 /** 0347 * Returns true if this is an IPv6 link-local address. 0348 */ 0349 inline bool isLinkLocal() const 0350 { 0351 if (version() != 6) { 0352 return false; 0353 } 0354 quint8 *addr = (quint8 *)this->addr(); 0355 return (addr[0] & 0xff) == 0xfe && 0356 (addr[1] & 0xc0) == 0x80; 0357 } 0358 0359 /** 0360 * Returns true if this is an IPv6 site-local address. 0361 */ 0362 inline bool isSiteLocal() const 0363 { 0364 if (version() != 6) { 0365 return false; 0366 } 0367 quint8 *addr = (quint8 *)this->addr(); 0368 return (addr[0] & 0xff) == 0xfe && 0369 (addr[1] & 0xc0) == 0xc0; 0370 } 0371 0372 /** 0373 * Returns true if this is a global IPv6 address. 0374 */ 0375 inline bool isGlobal() const 0376 { 0377 return version() != 6 ? false : !(isMulticast() || isLinkLocal() || isSiteLocal()); 0378 } 0379 0380 /** 0381 * Returns true if this is a v4-mapped IPv6 address. 0382 */ 0383 inline bool isV4Mapped() const 0384 { 0385 if (version() != 6) { 0386 return false; 0387 } 0388 quint32 *addr = (quint32 *)this->addr(); 0389 return addr[0] == 0 && addr[1] == 0 && 0390 ((quint16 *)&addr[2])[0] == 0 && 0391 ((quint16 *)&addr[2])[1] == 0xffff; 0392 } 0393 0394 /** 0395 * Returns true if this is a v4-compat IPv6 address. 0396 */ 0397 inline bool isV4Compat() const 0398 { 0399 if (version() != 6 || isLocalhost()) { 0400 return false; 0401 } 0402 quint32 *addr = (quint32 *)this->addr(); 0403 return addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] != 0; 0404 } 0405 0406 /** 0407 * Returns true if this is an IPv6 node-local multicast address. 0408 */ 0409 inline bool isMulticastNodeLocal() const 0410 { 0411 return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0x1; 0412 } 0413 0414 /** 0415 * Returns true if this is an IPv6 link-local multicast address. 0416 */ 0417 inline bool isMulticastLinkLocal() const 0418 { 0419 return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0x2; 0420 } 0421 0422 /** 0423 * Returns true if this is an IPv6 site-local multicast address. 0424 */ 0425 inline bool isMulticastSiteLocal() const 0426 { 0427 return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0x5; 0428 } 0429 0430 /** 0431 * Returns true if this is an IPv6 organisational-local multicast address. 0432 */ 0433 inline bool isMulticastOrgLocal() const 0434 { 0435 return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0x8; 0436 } 0437 0438 /** 0439 * Returns true if this is an IPv6 global multicast address. 0440 */ 0441 inline bool isMulticastGlobal() const 0442 { 0443 return version() == 6 && isMulticast() && (((quint32 *)addr())[0] & 0xf) == 0xe; 0444 } 0445 0446 protected: 0447 quint32 m_data[4]; // 16 bytes, needed for an IPv6 address 0448 0449 char m_version; 0450 0451 public: 0452 /// localhost in IPv4 (127.0.0.1) 0453 static const KIpAddress localhostV4; 0454 /// the any host or undefined address in IPv4 (0.0.0.0) 0455 static const KIpAddress anyhostV4; 0456 0457 /// localhost in IPv6 (::1) 0458 static const KIpAddress localhostV6; 0459 /// the any host or undefined address in IPv6 (::) 0460 static const KIpAddress anyhostV6; 0461 }; 0462 0463 class KSocketAddressData; 0464 /** @class KSocketAddress k3socketaddress.h k3socketaddress.h 0465 * @brief A generic socket address. 0466 * 0467 * This class holds one generic socket address. 0468 * 0469 * @author Thiago Macieira <thiago@kde.org> 0470 * @deprecated Use KSocketFactory or KLocalSocket instead 0471 */ 0472 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KSocketAddress //krazy:exclude=dpointer (we got one, just not called Private) 0473 { 0474 public: 0475 /** 0476 * Default constructor. 0477 * 0478 * Creates an empty object 0479 */ 0480 KSocketAddress(); 0481 0482 /** 0483 * Creates this object with the given data. 0484 * The raw socket address is copied into this object. 0485 * 0486 * @param sa the socket address structure 0487 * @param len the socket address length 0488 */ 0489 KSocketAddress(const sockaddr *sa, quint16 len); 0490 0491 /** 0492 * Copy constructor. This creates a copy of the other 0493 * object. 0494 * 0495 * Data is not shared. 0496 * 0497 * @param other the object to copy from 0498 */ 0499 KSocketAddress(const KSocketAddress &other); 0500 0501 /** 0502 * Destructor. Frees any associated resources. 0503 */ 0504 virtual ~KSocketAddress(); 0505 0506 /** 0507 * Performs a shallow copy of the other object into this one. 0508 * Data will be copied. 0509 * 0510 * @param other the object to copy from 0511 */ 0512 KSocketAddress &operator =(const KSocketAddress &other); 0513 0514 /** 0515 * Returns the socket address structure, to be passed down to 0516 * low level functions. 0517 * 0518 * Note that this function returns NULL for invalid or empty sockets, 0519 * so you may use to to test for validity. 0520 */ 0521 const sockaddr *address() const; 0522 0523 /** 0524 * Returns the socket address structure, to be passed down to 0525 * low level functions. 0526 * 0527 * Note that this function returns NULL for invalid or empty sockets, 0528 * so you may use to to test for validity. 0529 * 0530 * The returned value, if not NULL, is an internal buffer which is guaranteed 0531 * to be at least length() bytes long. 0532 */ 0533 sockaddr *address(); 0534 0535 /** 0536 * Sets the address to the given address. 0537 * The raw socket address is copied into this object. 0538 * 0539 * @param sa the socket address structure 0540 * @param len the socket address length 0541 */ 0542 KSocketAddress &setAddress(const sockaddr *sa, quint16 len); 0543 0544 /** 0545 * Returns the socket address structure, to be passed down to 0546 * low level functions. 0547 */ 0548 inline operator const sockaddr *() const 0549 { 0550 return address(); 0551 } 0552 0553 /** 0554 * Returns the length of this socket address structure. 0555 */ 0556 quint16 length() const; 0557 0558 /** 0559 * Sets the length of this socket structure. 0560 * 0561 * Use this function with care. It allows you to resize the internal 0562 * buffer to fit needs. This function should not be used except for handling 0563 * unknown socket address structures. 0564 * 0565 * Also note that this function may invalidate the socket if a known 0566 * family is set (Internet or Unix socket) and the new length would be 0567 * too small to hold the system's sockaddr_* structure. If unsure, reset 0568 * the family: 0569 * 0570 * \code 0571 * KSocketAddress qsa; 0572 * [...] 0573 * qsa.setFamily(AF_UNSPEC).setLength(newlen); 0574 * \endcode 0575 * 0576 * @param len the new length 0577 */ 0578 KSocketAddress &setLength(quint16 len); 0579 0580 /** 0581 * Returns the family of this address. 0582 * @return the family of this address, AF_UNSPEC if it's undefined 0583 */ 0584 int family() const; 0585 0586 /** 0587 * Sets the family of this object. 0588 * 0589 * Note: setting the family will probably invalidate any address data 0590 * contained in this object. Use this function with care. 0591 * 0592 * @param family the new family to set 0593 */ 0594 virtual KSocketAddress &setFamily(int family); 0595 0596 /** 0597 * Returns the IANA family number of this address. 0598 * @return the IANA family number of this address (1 for AF_INET. 0599 * 2 for AF_INET6, otherwise 0) 0600 */ 0601 inline int ianaFamily() const 0602 { 0603 return ianaFamily(family()); 0604 } 0605 0606 /** 0607 * Returns true if this equals the other socket. 0608 * 0609 * Socket addresses are considered matching if and only if all data is the same. 0610 * 0611 * @param other the other socket 0612 * @return true if both sockets are equal 0613 */ 0614 bool operator ==(const KSocketAddress &other) const; 0615 0616 /** 0617 * Returns the node name of this socket. 0618 * 0619 * In the case of Internet sockets, this is string representation of the IP address. 0620 * The default implementation returns QString(). 0621 * 0622 * @return the node name, can be QString() 0623 * @bug use KResolver to resolve unknown families 0624 */ 0625 virtual QString nodeName() const; 0626 0627 /** 0628 * Returns the service name for this socket. 0629 * 0630 * In the case of Internet sockets, this is the port number. 0631 * The default implementation returns QString(). 0632 * 0633 * @return the service name, can be QString() 0634 * @bug use KResolver to resolve unknown families 0635 */ 0636 virtual QString serviceName() const; 0637 0638 /** 0639 * Returns this socket address as a string suitable for 0640 * printing. Family, node and service are part of this address. 0641 * 0642 * @bug use KResolver to resolve unknown families 0643 */ 0644 virtual QString toString() const; 0645 0646 /** 0647 * Returns an object reference that can be used to manipulate this socket 0648 * as an Internet socket address. Both objects share the same data. 0649 */ 0650 KInetSocketAddress &asInet(); 0651 0652 /** 0653 * Returns an object is equal to this object's data, but they don't share it. 0654 */ 0655 KInetSocketAddress asInet() const; 0656 0657 /** 0658 * Returns an object reference that can be used to manipulate this socket 0659 * as a Unix socket address. Both objects share the same data. 0660 */ 0661 KUnixSocketAddress &asUnix(); 0662 0663 /** 0664 * Returns an object is equal to this object's data, but they don't share it. 0665 */ 0666 KUnixSocketAddress asUnix() const; 0667 0668 protected: 0669 /// @internal 0670 /// private data 0671 KSocketAddressData *d; 0672 0673 /// @internal 0674 /// extra constructor 0675 KSocketAddress(KSocketAddressData *d); 0676 0677 public: // static 0678 /** 0679 * Returns the IANA family number of the given address family. 0680 * Returns 0 if there is no corresponding IANA family number. 0681 * @param af the address family, in AF_* constants 0682 * @return the IANA family number of this address (1 for AF_INET. 0683 * 2 for AF_INET6, otherwise 0) 0684 */ 0685 static int ianaFamily(int af); 0686 0687 /** 0688 * Returns the address family of the given IANA family number. 0689 * @return the address family, AF_UNSPEC for unknown IANA family numbers 0690 */ 0691 static int fromIanaFamily(int iana); 0692 }; 0693 0694 /** @class KInetSocketAddress k3socketaddress.h k3socketaddress.h 0695 * @brief an Internet socket address 0696 * 0697 * An Inet (IPv4 or IPv6) socket address 0698 * 0699 * This is an IPv4 or IPv6 address of the Internet. 0700 * 0701 * @author Thiago Macieira <thiago@kde.org> 0702 * @deprecated Use KSocketFactory or KLocalSocket instead 0703 */ 0704 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KInetSocketAddress: public KSocketAddress 0705 { 0706 friend class KSocketAddress; 0707 public: 0708 /** 0709 * Public constructor. Creates an empty object. 0710 */ 0711 KInetSocketAddress(); 0712 0713 /** 0714 * Creates an object from raw data. 0715 * 0716 * Note: if the socket address @p sa does not contain a valid Internet 0717 * socket (IPv4 or IPv6), this object will be empty. 0718 * 0719 * @param sa the sockaddr structure 0720 * @param len the structure's length 0721 */ 0722 KInetSocketAddress(const sockaddr *sa, quint16 len); 0723 0724 /** 0725 * Creates an object from an IP address and port. 0726 * 0727 * @param host the IP address 0728 * @param port the port number 0729 */ 0730 KInetSocketAddress(const KIpAddress &host, quint16 port); 0731 0732 /** 0733 * Copy constructor. 0734 * 0735 * Data is not shared. 0736 * 0737 * @param other the other object 0738 */ 0739 KInetSocketAddress(const KInetSocketAddress &other); 0740 0741 /** 0742 * Copy constructor. 0743 * 0744 * If the other, generic socket address contains an Internet address, 0745 * it will be copied. Otherwise, this object will be empty. 0746 * 0747 * @param other the other object 0748 */ 0749 KInetSocketAddress(const KSocketAddress &other); 0750 0751 /** 0752 * Destroys this object. 0753 */ 0754 ~KInetSocketAddress() override; 0755 0756 /** 0757 * Copy operator. 0758 * 0759 * Copies the other object into this one. 0760 * 0761 * @param other the other object 0762 */ 0763 KInetSocketAddress &operator =(const KInetSocketAddress &other); 0764 0765 /** 0766 * Cast operator to sockaddr_in. 0767 */ 0768 inline operator const sockaddr_in *() const 0769 { 0770 return (const sockaddr_in *)address(); 0771 } 0772 0773 /** 0774 * Cast operator to sockaddr_in6. 0775 */ 0776 inline operator const sockaddr_in6 *() const 0777 { 0778 return (const sockaddr_in6 *)address(); 0779 } 0780 0781 /** 0782 * Returns the IP version of the address this object holds. 0783 * 0784 * @return 4 or 6, if IPv4 or IPv6, respectively; 0 if this object is empty 0785 */ 0786 int ipVersion() const; 0787 0788 /** 0789 * Returns the IP address component. 0790 */ 0791 KIpAddress ipAddress() const; 0792 0793 /** 0794 * Sets the IP address to the given raw address. 0795 * 0796 * This call will preserve port numbers across IP versions, but will lose 0797 * IPv6 specific data if the address is set to IPv4. 0798 * 0799 * @param addr the address to set to 0800 * @return a reference to itself 0801 */ 0802 KInetSocketAddress &setHost(const KIpAddress &addr); 0803 0804 /** 0805 * Retrieves the port number stored in this object. 0806 * 0807 * @return a port number in the range 0 to 65535, inclusive. An empty or 0808 * invalid object will have a port number of 0. 0809 */ 0810 quint16 port() const; 0811 0812 /** 0813 * Sets the port number. If this object is empty, this function will default to 0814 * creating an IPv4 address. 0815 * 0816 * @param port the port number to set 0817 * @return a reference to itself 0818 */ 0819 KInetSocketAddress &setPort(quint16 port); 0820 0821 /** 0822 * Converts this object to an IPv4 socket address. It has no effect if the object 0823 * is already an IPv4 socket address. 0824 * 0825 * If this object is an IPv6 address, the port number is preserved. All other information 0826 * is lost. 0827 * 0828 * @return a reference to itself 0829 */ 0830 KInetSocketAddress &makeIPv4(); 0831 0832 /** 0833 * Converts this object to an IPv6 socket address. It has no effect if the object 0834 * is already an IPv6 socket address. 0835 * 0836 * If this object is an IPv4 address, the port number is preserved. 0837 * 0838 * @return a reference to itself 0839 */ 0840 KInetSocketAddress &makeIPv6(); 0841 0842 /** 0843 * Returns the flowinfo information from the IPv6 socket address. 0844 * 0845 * @return the flowinfo information or 0 if this object is empty or IPv4 0846 */ 0847 quint32 flowinfo() const; 0848 0849 /** 0850 * Sets the flowinfo information for an IPv6 socket address. If this is not 0851 * an IPv6 socket address, this function converts it to one. See makeIPv6. 0852 * 0853 * @param flowinfo the flowinfo to set 0854 * @return a reference to itself 0855 */ 0856 KInetSocketAddress &setFlowinfo(quint32 flowinfo); 0857 0858 /** 0859 * Returns the scope id this IPv6 socket is bound to. 0860 * 0861 * @return the scope id, or 0 if this is not an IPv6 object 0862 */ 0863 int scopeId() const; 0864 0865 /** 0866 * Sets the scope id for this IPv6 object. If this is not an IPv6 socket 0867 * address, this function converts it to one. See makeIPv6 0868 * 0869 * @param scopeid the scopeid to set 0870 * @return a reference to itself 0871 */ 0872 KInetSocketAddress &setScopeId(int scopeid); 0873 0874 protected: 0875 /// @internal 0876 /// extra constructor 0877 KInetSocketAddress(KSocketAddressData *d); 0878 0879 private: 0880 void update(); 0881 }; 0882 0883 /* 0884 * External definition 0885 */ 0886 0887 /** @class KUnixSocketAddress k3socketaddress.h k3socketaddress.h 0888 * @brief A Unix (local) socket address. 0889 * 0890 * This is a Unix socket address. 0891 * 0892 * Note that this class uses QStrings to represent filenames, which means 0893 * the proper encoding is used to translate into valid filesystem file names. 0894 * 0895 * @author Thiago Macieira <thiago@kde.org> 0896 * @deprecated Use KSocketFactory or KLocalSocket instead 0897 */ 0898 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KUnixSocketAddress: public KSocketAddress 0899 { 0900 friend class KSocketAddress; 0901 public: 0902 /** 0903 * Default constructor. Creates an empty object. 0904 */ 0905 KUnixSocketAddress(); 0906 0907 /** 0908 * Creates this object with the given raw data. If 0909 * the sockaddr structure does not contain a Local namespace 0910 * (Unix) socket, this object will be created empty. 0911 * 0912 * @param sa the socket address structure 0913 * @param len the structure's length 0914 */ 0915 KUnixSocketAddress(const sockaddr *sa, quint16 len); 0916 0917 /** 0918 * Copy constructor. Creates a copy of the other object, 0919 * sharing the data explicitly. 0920 * 0921 * @param other the other object 0922 */ 0923 KUnixSocketAddress(const KUnixSocketAddress &other); 0924 0925 /** 0926 * Constructs an object from the given pathname. 0927 */ 0928 KUnixSocketAddress(const QString &pathname); 0929 0930 /** 0931 * Destructor. 0932 */ 0933 ~KUnixSocketAddress() override; 0934 0935 /** 0936 * Copy operator. Copies the contents of the other object into 0937 * this one. Data is explicitly shared. 0938 * 0939 * @param other the other 0940 */ 0941 KUnixSocketAddress &operator =(const KUnixSocketAddress &other); 0942 0943 /** 0944 * Cast operator to sockaddr_un. 0945 */ 0946 inline operator const sockaddr_un *() const 0947 { 0948 return (const sockaddr_un *)address(); 0949 } 0950 0951 /** 0952 * Returns the pathname associated with this object. Will return 0953 * QString() if this object is empty. 0954 */ 0955 QString pathname() const; 0956 0957 /** 0958 * Sets the pathname for the object. 0959 * 0960 * @return a reference to itself 0961 */ 0962 KUnixSocketAddress &setPathname(const QString &path); 0963 0964 protected: 0965 /// @internal 0966 /// extra constructor 0967 KUnixSocketAddress(KSocketAddressData *d); 0968 }; 0969 0970 } // namespace KNetwork 0971 0972 #endif