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
00061 class KDECORE_EXPORT KIpAddress
00062 {
00063 public:
00068 inline KIpAddress() : m_version(0)
00069 { }
00070
00079 inline KIpAddress(const KIpAddress& other)
00080 { *this = other; }
00081
00089 inline KIpAddress(const QString& addr)
00090 { setAddress(addr); }
00091
00099 inline KIpAddress(const char* addr)
00100 { setAddress(addr); }
00101
00108 inline KIpAddress(const void* addr, int version = 4)
00109 { setAddress(addr, version); }
00110
00121 inline KIpAddress(quint32 ip4addr)
00122 { setAddress(&ip4addr, 4); }
00123
00130 inline ~KIpAddress()
00131 { }
00132
00140 KIpAddress& operator =(const KIpAddress& other);
00141
00147 inline bool operator ==(const KIpAddress& other) const
00148 { return compare(other, true); }
00149
00163 bool compare(const KIpAddress& other, bool checkMapped = true) const;
00164
00170 inline int version() const
00171 { return m_version; }
00172
00176 inline bool isIPv4Addr() const
00177 { return version() == 4; }
00178
00182 inline bool isIPv6Addr() const
00183 { return version() == 6; }
00184
00191 bool setAddress(const QString& address);
00192
00199 bool setAddress(const char* address);
00200
00209 bool setAddress(const void* raw, int version = 4);
00210
00214 QString toString() const;
00215
00219 inline const void *addr() const
00220 { return m_data; }
00221
00235 inline quint32 IPv4Addr(bool convertMapped = true) const
00236 {
00237 return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0];
00238 }
00239
00240
00241
00245 inline bool isUnspecified() const
00246 { return version() == 0 ? true : (*this == anyhostV4 || *this == anyhostV6); }
00247
00251 inline bool isLocalhost() const
00252 { return version() == 0 ? false : (*this == localhostV4 || *this == localhostV6); }
00253
00257 inline bool isLoopback() const
00258 { return isLocalhost(); }
00259
00266 inline bool isClassA() const
00267 { return version() != 4 ? false : (IPv4Addr() & 0x80000000) == 0; }
00268
00275 inline bool isClassB() const
00276 { return version() != 4 ? false : (IPv4Addr() & 0xc0000000) == 0x80000000; }
00277
00284 inline bool isClassC() const
00285 { return version() != 4 ? false : (IPv4Addr() & 0xe0000000) == 0xc0000000; }
00286
00293 inline bool isClassD() const
00294 { return version() != 4 ? false : (IPv4Addr() & 0xf0000000) == 0xe0000000; }
00295
00299 inline bool isMulticast() const
00300 {
00301 if (version() == 4) return isClassD();
00302 if (version() == 6) return ((quint8*)addr())[0] == 0xff;
00303 return false;
00304 }
00305
00309 inline bool isLinkLocal() const
00310 {
00311 if (version() != 6) return false;
00312 quint8* addr = (quint8*)this->addr();
00313 return (addr[0] & 0xff) == 0xfe &&
00314 (addr[1] & 0xc0) == 0x80;
00315 }
00316
00320 inline bool isSiteLocal() const
00321 {
00322 if (version() != 6) return false;
00323 quint8* addr = (quint8*)this->addr();
00324 return (addr[0] & 0xff) == 0xfe &&
00325 (addr[1] & 0xc0) == 0xc0;
00326 }
00327
00331 inline bool isGlobal() const
00332 { return version() != 6 ? false : !(isMulticast() || isLinkLocal() || isSiteLocal()); }
00333
00337 inline bool isV4Mapped() const
00338 {
00339 if (version() != 6) return false;
00340 quint32* addr = (quint32*)this->addr();
00341 return addr[0] == 0 && addr[1] == 0 &&
00342 ((quint16*)&addr[2])[0] == 0 &&
00343 ((quint16*)&addr[2])[1] == 0xffff;
00344 }
00345
00349 inline bool isV4Compat() const
00350 {
00351 if (version() != 6 || isLocalhost()) return false;
00352 quint32* addr = (quint32*)this->addr();
00353 return addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] != 0;
00354 }
00355
00359 inline bool isMulticastNodeLocal() const
00360 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x1; }
00361
00365 inline bool isMulticastLinkLocal() const
00366 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x2; }
00367
00371 inline bool isMulticastSiteLocal() const
00372 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x5; }
00373
00377 inline bool isMulticastOrgLocal() const
00378 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0x8; }
00379
00383 inline bool isMulticastGlobal() const
00384 { return version() == 6 && isMulticast() && (((quint32*)addr())[0] & 0xf) == 0xe; }
00385
00386 protected:
00387 quint32 m_data[4];
00388
00389 char m_version;
00390
00391 public:
00393 static const KIpAddress localhostV4;
00395 static const KIpAddress anyhostV4;
00396
00398 static const KIpAddress localhostV6;
00400 static const KIpAddress anyhostV6;
00401 };
00402
00403
00404 class KSocketAddressData;
00412 class KDECORE_EXPORT KSocketAddress
00413 {
00414 public:
00420 KSocketAddress();
00421
00429 KSocketAddress(const sockaddr* sa, quint16 len);
00430
00439 KSocketAddress(const KSocketAddress& other);
00440
00444 virtual ~KSocketAddress();
00445
00452 KSocketAddress& operator =(const KSocketAddress& other);
00453
00461 const sockaddr* address() const;
00462
00473 sockaddr* address();
00474
00482 KSocketAddress& setAddress(const sockaddr *sa, quint16 len);
00483
00488 inline operator const sockaddr*() const
00489 { return address(); }
00490
00494 quint16 length() const;
00495
00516 KSocketAddress& setLength(quint16 len);
00517
00522 int family() const;
00523
00532 virtual KSocketAddress& setFamily(int family);
00533
00539 inline int ianaFamily() const
00540 { return ianaFamily(family()); }
00541
00550 bool operator ==(const KSocketAddress& other) const;
00551
00561 virtual QString nodeName() const;
00562
00572 virtual QString serviceName() const;
00573
00580 virtual QString toString() const;
00581
00586 KInetSocketAddress& asInet();
00587
00591 KInetSocketAddress asInet() const;
00592
00597 KUnixSocketAddress& asUnix();
00598
00602 KUnixSocketAddress asUnix() const;
00603
00604 protected:
00607 KSocketAddressData *d;
00608
00611 KSocketAddress(KSocketAddressData* d);
00612
00613 public:
00621 static int ianaFamily(int af);
00622
00627 static int fromIanaFamily(int iana);
00628 };
00629
00630
00640 class KDECORE_EXPORT KInetSocketAddress: public KSocketAddress
00641 {
00642 friend class KSocketAddress;
00643 public:
00647 KInetSocketAddress();
00648
00658 KInetSocketAddress(const sockaddr* sa, quint16 len);
00659
00666 KInetSocketAddress(const KIpAddress& host, quint16 port);
00667
00675 KInetSocketAddress(const KInetSocketAddress& other);
00676
00685 KInetSocketAddress(const KSocketAddress& other);
00686
00690 virtual ~KInetSocketAddress();
00691
00699 KInetSocketAddress& operator =(const KInetSocketAddress& other);
00700
00704 inline operator const sockaddr_in*() const
00705 { return (const sockaddr_in*)address(); }
00706
00710 inline operator const sockaddr_in6*() const
00711 { return (const sockaddr_in6*)address(); }
00712
00718 int ipVersion() const;
00719
00723 KIpAddress ipAddress() const;
00724
00734 KInetSocketAddress& setHost(const KIpAddress& addr);
00735
00742 quint16 port() const;
00743
00751 KInetSocketAddress& setPort(quint16 port);
00752
00762 KInetSocketAddress& makeIPv4();
00763
00772 KInetSocketAddress& makeIPv6();
00773
00779 quint32 flowinfo() const;
00780
00788 KInetSocketAddress& setFlowinfo(quint32 flowinfo);
00789
00795 int scopeId() const;
00796
00804 KInetSocketAddress& setScopeId(int scopeid);
00805
00806 protected:
00809 KInetSocketAddress(KSocketAddressData* d);
00810
00811 private:
00812 void update();
00813 };
00814
00815
00816
00817
00818
00829 class KDECORE_EXPORT KUnixSocketAddress: public KSocketAddress
00830 {
00831 friend class KSocketAddress;
00832 public:
00836 KUnixSocketAddress();
00837
00846 KUnixSocketAddress(const sockaddr* sa, quint16 len);
00847
00854 KUnixSocketAddress(const KUnixSocketAddress& other);
00855
00859 KUnixSocketAddress(const QString& pathname);
00860
00864 virtual ~KUnixSocketAddress();
00865
00872 KUnixSocketAddress& operator =(const KUnixSocketAddress& other);
00873
00877 inline operator const sockaddr_un*() const
00878 { return (const sockaddr_un*)address(); }
00879
00884 QString pathname() const;
00885
00891 KUnixSocketAddress& setPathname(const QString& path);
00892
00893 protected:
00896 KUnixSocketAddress(KSocketAddressData* d);
00897 };
00898
00899 }
00900
00901 #endif