KDECore
k3socketaddress.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef KSOCKETADDRESS_H
00027 #define KSOCKETADDRESS_H
00028
00029 #include <kdecore_export.h>
00030 #include <QtCore/QByteArray>
00031
00032 struct sockaddr;
00033 struct sockaddr_in;
00034 struct sockaddr_in6;
00035 struct sockaddr_un;
00036
00037 namespace KNetwork {
00038
00039 class KIpAddress;
00040 class KSocketAddress;
00041 class KInetSocketAddress;
00042 class KUnixSocketAddress;
00043
00062 class KDECORE_EXPORT KIpAddress
00063 {
00064 public:
00069 inline KIpAddress() : m_version(0)
00070 { }
00071
00080 inline KIpAddress(const KIpAddress& other)
00081 { *this = other; }
00082
00090 inline KIpAddress(const QString& addr)
00091 { setAddress(addr); }
00092
00100 inline KIpAddress(const char* addr)
00101 { setAddress(addr); }
00102
00109 inline KIpAddress(const void* addr, int version = 4)
00110 { setAddress(addr, version); }
00111
00122 inline KIpAddress(quint32 ip4addr)
00123 { setAddress(&ip4addr, 4); }
00124
00131 inline ~KIpAddress()
00132 { }
00133
00141 KIpAddress& operator =(const KIpAddress& other);
00142
00148 inline bool operator ==(const KIpAddress& other) const
00149 { return compare(other, true); }
00150
00164 bool compare(const KIpAddress& other, bool checkMapped = true) const;
00165
00171 inline int version() const
00172 { return m_version; }
00173
00177 inline bool isIPv4Addr() const
00178 { return version() == 4; }
00179
00183 inline bool isIPv6Addr() const
00184 { return version() == 6; }
00185
00192 bool setAddress(const QString& address);
00193
00200 bool setAddress(const char* address);
00201
00210 bool setAddress(const void* raw, int version = 4);
00211
00215 QString toString() const;
00216
00220 inline const void *addr() const
00221 { return m_data; }
00222
00236 inline quint32 IPv4Addr(bool convertMapped = true) const
00237 {
00238 return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0];
00239 }
00240
00241
00242
00246 inline bool isUnspecified() const
00247 { return version() == 0 ? true : (*this == anyhostV4 || *this == anyhostV6); }
00248
00252 inline bool isLocalhost() const
00253 { return version() == 0 ? false : (*this == localhostV4 || *this == localhostV6); }
00254
00258 inline bool isLoopback() const
00259 { return isLocalhost(); }
00260
00267 inline bool isClassA() const
00268 { return version() != 4 ? false : (IPv4Addr() & 0x80000000) == 0; }
00269
00276 inline bool isClassB() const
00277 { return version() != 4 ? false : (IPv4Addr() & 0xc0000000) == 0x80000000; }
00278
00285 inline bool isClassC() const
00286 { return version() != 4 ? false : (IPv4Addr() & 0xe0000000) == 0xc0000000; }
00287
00294 inline bool isClassD() const
00295 { return version() != 4 ? false : (IPv4Addr() & 0xf0000000) == 0xe0000000; }
00296
00300 inline bool isMulticast() const
00301 {
00302 if (version() == 4) return isClassD();
00303 if (version() == 6) return ((quint8*)addr())[0] == 0xff;
00304 return false;
00305 }
00306
00310 inline bool isLinkLocal() const
00311 {
00312 if (version() != 6) return false;
00313 quint8* addr = (quint8*)this->addr();
00314 return (addr[0] & 0xff) == 0xfe &&
00315 (addr[1] & 0xc0) == 0x80;
00316 }
00317
00321 inline bool isSiteLocal() const
00322 {
00323 if (version() != 6) return false;
00324 quint8* addr = (quint8*)this->addr();
00325 return (addr[0] & 0xff) == 0xfe &&
00326 (addr[1] & 0xc0) == 0xc0;
00327 }
00328
00332 inline bool isGlobal() const
00333 { return version() != 6 ? false : !(isMulticast() || isLinkLocal() || isSiteLocal()); }
00334
00338 inline bool isV4Mapped() const
00339 {
00340 if (version() != 6) return false;
00341 quint32* addr = (quint32*)this->addr();
00342 return addr[0] == 0 && addr[1] == 0 &&
00343 ((quint16*)&addr[2])[0] == 0 &&
00344 ((quint16*)&addr[2])[1] == 0xffff;
00345 }
00346
00350 inline bool isV4Compat() const
00351 {
00352 if (version() != 6 || isLocalhost()) return false;
00353 quint32* addr = (quint32*)this->addr();
00354 return addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] != 0;
00355 }
00356
00360 inline bool isMulticastNodeLocal() const
00361 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x1; }
00362
00366 inline bool isMulticastLinkLocal() const
00367 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x2; }
00368
00372 inline bool isMulticastSiteLocal() const
00373 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x5; }
00374
00378 inline bool isMulticastOrgLocal() const
00379 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x8; }
00380
00384 inline bool isMulticastGlobal() const
00385 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0xe; }
00386
00387 protected:
00388 quint32 m_data[4];
00389
00390 char m_version;
00391
00392 public:
00394 static const KIpAddress localhostV4;
00396 static const KIpAddress anyhostV4;
00397
00399 static const KIpAddress localhostV6;
00401 static const KIpAddress anyhostV6;
00402 };
00403
00404
00405 class KSocketAddressData;
00414 class KDECORE_EXPORT KSocketAddress
00415 {
00416 public:
00422 KSocketAddress();
00423
00431 KSocketAddress(const sockaddr* sa, quint16 len);
00432
00441 KSocketAddress(const KSocketAddress& other);
00442
00446 virtual ~KSocketAddress();
00447
00454 KSocketAddress& operator =(const KSocketAddress& other);
00455
00463 const sockaddr* address() const;
00464
00475 sockaddr* address();
00476
00484 KSocketAddress& setAddress(const sockaddr *sa, quint16 len);
00485
00490 inline operator const sockaddr*() const
00491 { return address(); }
00492
00496 quint16 length() const;
00497
00518 KSocketAddress& setLength(quint16 len);
00519
00524 int family() const;
00525
00534 virtual KSocketAddress& setFamily(int family);
00535
00541 inline int ianaFamily() const
00542 { return ianaFamily(family()); }
00543
00552 bool operator ==(const KSocketAddress& other) const;
00553
00563 virtual QString nodeName() const;
00564
00574 virtual QString serviceName() const;
00575
00582 virtual QString toString() const;
00583
00588 KInetSocketAddress& asInet();
00589
00593 KInetSocketAddress asInet() const;
00594
00599 KUnixSocketAddress& asUnix();
00600
00604 KUnixSocketAddress asUnix() const;
00605
00606 protected:
00609 KSocketAddressData *d;
00610
00613 KSocketAddress(KSocketAddressData* d);
00614
00615 public:
00623 static int ianaFamily(int af);
00624
00629 static int fromIanaFamily(int iana);
00630 };
00631
00632
00643 class KDECORE_EXPORT KInetSocketAddress: public KSocketAddress
00644 {
00645 friend class KSocketAddress;
00646 public:
00650 KInetSocketAddress();
00651
00661 KInetSocketAddress(const sockaddr* sa, quint16 len);
00662
00669 KInetSocketAddress(const KIpAddress& host, quint16 port);
00670
00678 KInetSocketAddress(const KInetSocketAddress& other);
00679
00688 KInetSocketAddress(const KSocketAddress& other);
00689
00693 virtual ~KInetSocketAddress();
00694
00702 KInetSocketAddress& operator =(const KInetSocketAddress& other);
00703
00707 inline operator const sockaddr_in*() const
00708 { return (const sockaddr_in*)address(); }
00709
00713 inline operator const sockaddr_in6*() const
00714 { return (const sockaddr_in6*)address(); }
00715
00721 int ipVersion() const;
00722
00726 KIpAddress ipAddress() const;
00727
00737 KInetSocketAddress& setHost(const KIpAddress& addr);
00738
00745 quint16 port() const;
00746
00754 KInetSocketAddress& setPort(quint16 port);
00755
00765 KInetSocketAddress& makeIPv4();
00766
00775 KInetSocketAddress& makeIPv6();
00776
00782 quint32 flowinfo() const;
00783
00791 KInetSocketAddress& setFlowinfo(quint32 flowinfo);
00792
00798 int scopeId() const;
00799
00807 KInetSocketAddress& setScopeId(int scopeid);
00808
00809 protected:
00812 KInetSocketAddress(KSocketAddressData* d);
00813
00814 private:
00815 void update();
00816 };
00817
00818
00819
00820
00821
00833 class KDECORE_EXPORT KUnixSocketAddress: public KSocketAddress
00834 {
00835 friend class KSocketAddress;
00836 public:
00840 KUnixSocketAddress();
00841
00850 KUnixSocketAddress(const sockaddr* sa, quint16 len);
00851
00858 KUnixSocketAddress(const KUnixSocketAddress& other);
00859
00863 KUnixSocketAddress(const QString& pathname);
00864
00868 virtual ~KUnixSocketAddress();
00869
00876 KUnixSocketAddress& operator =(const KUnixSocketAddress& other);
00877
00881 inline operator const sockaddr_un*() const
00882 { return (const sockaddr_un*)address(); }
00883
00888 QString pathname() const;
00889
00895 KUnixSocketAddress& setPathname(const QString& path);
00896
00897 protected:
00900 KUnixSocketAddress(KSocketAddressData* d);
00901 };
00902
00903 }
00904
00905 #endif