ksocketaddress.h
来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C头文件 代码 · 共 903 行 · 第 1/2 页
H
903 行
/* -*- C++ -*- * Copyright (C) 2003 Thiago Macieira <thiago.macieira@kdemail.net> * * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */#ifndef KSOCKETADDRESS_H#define KSOCKETADDRESS_H#include <qstring.h>#include <qcstring.h>#include <kdelibs_export.h>struct sockaddr;struct sockaddr_in;struct sockaddr_in6;struct sockaddr_un;namespace KNetwork {class KIpAddress;class KSocketAddress;class KInetSocketAddress;class KUnixSocketAddress;/** @class KIpAddress ksocketaddress.h ksocketaddress.h * @brief An IP address. * * This class represents one IP address, version 4 or 6. This is only * the address, not including port information or other data. * * It is not a good programming practice to create address from objects * like this. Instead, prefer a more thorough function like * @ref KResolver::resolve, which also handle extra information like scope * ids. * * This is a light-weight class. Most of the member functions are inlined and * there are no virtual functions. This object's size should be less than 20 * bytes. Also note that there is no sharing of data. * * @author Thiago Macieira <thiago.macieira@kdemail.net> */class KDECORE_EXPORT KIpAddress{public: /** * Default constructor. Creates an empty address. * It defaults to IP version 4. */ inline KIpAddress() : m_version(0) { } /** * Copy constructor. Copies the data from the other * object. * * Data is not shared. * * @param other the other */ inline KIpAddress(const KIpAddress& other) { *this = other; } /** * Creates an object from the given string representation. * * The IP version is guessed from the address format. * * @param addr the address */ inline KIpAddress(const QString& addr) { setAddress(addr); } /** * Creates an object from the given string representation. * * The IP version is guessed from the address format. * * @param addr the address */ inline KIpAddress(const char* addr) { setAddress(addr); } /** * Creates an object from the given raw data and IP version. * * @param addr the raw data * @param version the IP version (4 or 6) */ inline KIpAddress(const void* addr, int version = 4) { setAddress(addr, version); } /** * This is a convenience constructor. Constructs an object * from the given IPv4 address in the form of an integer. * * Note: do not write code to depend on IPv4 addresses being * integer types. Instead, treat them as a special type, like * a KIpAddress or the system's in_addr. * * @param ip4addr the IPv4 address */ inline KIpAddress(Q_UINT32 ip4addr) { setAddress(&ip4addr, 4); } /** * Destructor. This frees resources associated with this object. * * Note: destructor is non-virtual. The compiler will happily optimise it * out of the way. */ inline ~KIpAddress() { } /** * Copy operator. * * Copies the data from the other object into this one. * * @param other the object to copy */ KIpAddress& operator =(const KIpAddress& other); /** * Returns true if the two addresses match. * This function performs a v4-mapped check. * @see compare */ inline bool operator ==(const KIpAddress& other) const { return compare(other, true); } /** * Compares this address against the other, supplied one and return * true if they match. The @p checkMapped parameter controls whether * a check for an IPv6 v4-mapped address will be performed. * * An IPv6 v4-mapped address is an IPv6 address that is, for all purposes, * equivalent to an IPv4 one. The default behaviour of this function * is to take that into account. If you want a strict matching, * pass @b false to the @p checkMapped parameter. * * @param other the other IP address * @param checkMapped whether v4-mapped addresses will be taken into account */ bool compare(const KIpAddress& other, bool checkMapped = true) const; /** * Retrieves the IP version in this object. * * @returns the version: 4 or 6 */ inline int version() const { return m_version; } /** * Returns true if this is an IPv4 address. */ inline bool isIPv4Addr() const { return version() == 4; } /** * Returns true if this is an IPv6 address. */ inline bool isIPv6Addr() const { return version() == 6; } /** * Sets the address to the given string representation. * * @return true if the address was successfully parsed; otherwise returns * false and leaves the object unchanged. */ bool setAddress(const QString& address); /** * Sets the address to the given string representation. * * @return true if the address was successfully parsed; otherwise returns * false and leaves the object unchanged. */ bool setAddress(const char* address); /** * Sets the address to the given raw binary representation. * * @param raw a pointer to the raw binary data * @param version the IP version * @return true if the address was successfully parsed; otherwise returns * false and leaves the object unchanged. */ bool setAddress(const void* raw, int version = 4); /** * Returns the address as a string. */ QString toString() const; /** * Returns a pointer to binary raw data representing the address. */ inline const void *addr() const { return m_data; } /** * This is a convenience function. Returns the IPv4 address in a * 32-bit integer. The result is only valid if @ref isIPv4Addr returns * true. Alternatively, if the contained IPv6 address is a v4-mapped one * and the @p convertMapped parameter is true, the result will also be * valid. * * Note: you should not treat IP addresses as integers. Instead, * use types defined for that purpose, such as KIpAddress or the * system's in_addr type. * * @bug Check if byte ordering is done right */ inline Q_UINT32 IPv4Addr(bool convertMapped = true) const { return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0]; } /*-- tests --*/ /** * Returns true if this is the IPv4 or IPv6 unspecified address. */ inline bool isUnspecified() const { return version() == 0 ? true : (*this == anyhostV4 || *this == anyhostV6); } /** * Returns true if this is either the IPv4 or the IPv6 localhost address. */ inline bool isLocalhost() const { return version() == 0 ? false : (*this == localhostV4 || *this == localhostV6); } /** * This is an alias for @ref isLocalhost. */ inline bool isLoopback() const { return isLocalhost(); } /** * Returns true if this is an IPv4 class A address, i.e., * from 0.0.0.0 to 127.255.255.255. * * This function does not test for v4-mapped addresses. */ inline bool isClassA() const { return version() != 4 ? false : (IPv4Addr() & 0x80000000) == 0; } /** * Returns true if this is an IPv4 class B address, i.e., one from * 128.0.0.0 to 191.255.255.255. * * This function does not test for v4-mapped addresses. */ inline bool isClassB() const { return version() != 4 ? false : (IPv4Addr() & 0xc0000000) == 0x80000000; } /** * Returns true if this is an IPv4 class C address, i.e., one from * 192.0.0.0 to 223.255.255.255. * * This function does not test for v4-mapped addresses. */ inline bool isClassC() const { return version() != 4 ? false : (IPv4Addr() & 0xe0000000) == 0xc0000000; } /** * Returns true if this is an IPv4 class D (a.k.a. multicast) address. * * Note: this function is not the same as @ref isMulticast. isMulticast also * tests for IPv6 multicast addresses. */ inline bool isClassD() const { return version() != 4 ? false : (IPv4Addr() & 0xf0000000) == 0xe0000000; } /** * Returns true if this is a multicast address, be it IPv4 or IPv6. */ inline bool isMulticast() const { if (version() == 4) return isClassD(); if (version() == 6) return ((Q_UINT8*)addr())[0] == 0xff; return false; } /** * Returns true if this is an IPv6 link-local address. */ inline bool isLinkLocal() const { if (version() != 6) return false; Q_UINT8* addr = (Q_UINT8*)this->addr(); return (addr[0] & 0xff) == 0xfe && (addr[1] & 0xc0) == 0x80; } /** * Returns true if this is an IPv6 site-local address. */ inline bool isSiteLocal() const { if (version() != 6) return false; Q_UINT8* addr = (Q_UINT8*)this->addr(); return (addr[0] & 0xff) == 0xfe && (addr[1] & 0xc0) == 0xc0; } /** * Returns true if this is a global IPv6 address. */ inline bool isGlobal() const { return version() != 6 ? false : !(isMulticast() || isLinkLocal() || isSiteLocal()); } /** * Returns true if this is a v4-mapped IPv6 address. */ inline bool isV4Mapped() const { if (version() != 6) return false; Q_UINT32* addr = (Q_UINT32*)this->addr(); return addr[0] == 0 && addr[1] == 0 && ((Q_UINT16*)&addr[2])[0] == 0 && ((Q_UINT16*)&addr[2])[1] == 0xffff; } /** * Returns true if this is a v4-compat IPv6 address. */ inline bool isV4Compat() const { if (version() != 6 || isLocalhost()) return false; Q_UINT32* addr = (Q_UINT32*)this->addr(); return addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] != 0; } /** * Returns true if this is an IPv6 node-local multicast address. */ inline bool isMulticastNodeLocal() const { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x1; } /** * Returns true if this is an IPv6 link-local multicast address. */ inline bool isMulticastLinkLocal() const { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x2; } /** * Returns true if this is an IPv6 site-local multicast address. */ inline bool isMulticastSiteLocal() const { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x5; } /** * Returns true if this is an IPv6 organisational-local multicast address. */ inline bool isMulticastOrgLocal() const { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x8; } /** * Returns true if this is an IPv6 global multicast address. */ inline bool isMulticastGlobal() const { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0xe; }protected: Q_UINT32 m_data[4]; // 16 bytes, needed for an IPv6 address char m_version;public: /// localhost in IPv4 (127.0.0.1) static const KIpAddress localhostV4; /// the any host or undefined address in IPv4 (0.0.0.0) static const KIpAddress anyhostV4; /// localhost in IPv6 (::1) static const KIpAddress localhostV6; /// the any host or undefined address in IPv6 (::) static const KIpAddress anyhostV6;};class KSocketAddressData;/** @class KSocketAddress ksocketaddress.h ksocketaddress.h * @brief A generic socket address. * * This class holds one generic socket address. * * @author Thiago Macieira <thiago.macieira@kdemail.net> */class KDECORE_EXPORT KSocketAddress{public: /** * Default constructor. * * Creates an empty object */ KSocketAddress(); /** * Creates this object with the given data. * The raw socket address is copied into this object. * * @param sa the socket address structure * @param len the socket address length */ KSocketAddress(const sockaddr* sa, Q_UINT16 len); /** * Copy constructor. This creates a copy of the other * object. * * Data is not shared. * * @param other the object to copy from */ KSocketAddress(const KSocketAddress& other); /** * Destructor. Frees any associated resources. */ virtual ~KSocketAddress(); /** * Performs a shallow copy of the other object into this one. * Data will be copied. * * @param other the object to copy from */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?