kdecore Library API Documentation

ksocketaddress.h

00001 /* -*- C++ -*- 00002 * Copyright (C) 2003 Thiago Macieira <thiago.macieira@kdemail.net> 00003 * 00004 * 00005 * Permission is hereby granted, free of charge, to any person obtaining 00006 * a copy of this software and associated documentation files (the 00007 * "Software"), to deal in the Software without restriction, including 00008 * without limitation the rights to use, copy, modify, merge, publish, 00009 * distribute, sublicense, and/or sell copies of the Software, and to 00010 * permit persons to whom the Software is furnished to do so, subject to 00011 * the following conditions: 00012 * 00013 * The above copyright notice and this permission notice shall be included 00014 * in all copies or substantial portions of the Software. 00015 * 00016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00017 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00018 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00021 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00023 */ 00024 00025 #ifndef KSOCKETADDRESS_H 00026 #define KSOCKETADDRESS_H 00027 00028 #include <qstring.h> 00029 #include <qcstring.h> 00030 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 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(Q_UINT32 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 Q_UINT32 IPv4Addr(bool convertMapped = true) const 00236 { 00237 return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0]; 00238 } 00239 00240 /*-- tests --*/ 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 ((Q_UINT8*)addr())[0] == 0xff; 00303 return false; 00304 } 00305 00309 inline bool isLinkLocal() const 00310 { 00311 if (version() != 6) return false; 00312 Q_UINT8* addr = (Q_UINT8*)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 Q_UINT8* addr = (Q_UINT8*)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 Q_UINT32* addr = (Q_UINT32*)this->addr(); 00341 return addr[0] == 0 && addr[1] == 0 && 00342 ((Q_UINT16*)&addr[2])[0] == 0 && 00343 ((Q_UINT16*)&addr[2])[1] == 0xffff; 00344 } 00345 00349 inline bool isV4Compat() const 00350 { 00351 if (version() != 6 || isLocalhost()) return false; 00352 Q_UINT32* addr = (Q_UINT32*)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() && (((Q_UINT32*)addr())[0] & 0xf) == 0x1; } 00361 00365 inline bool isMulticastLinkLocal() const 00366 { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x2; } 00367 00371 inline bool isMulticastSiteLocal() const 00372 { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x5; } 00373 00377 inline bool isMulticastOrgLocal() const 00378 { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x8; } 00379 00383 inline bool isMulticastGlobal() const 00384 { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0xe; } 00385 00386 protected: 00387 Q_UINT32 m_data[4]; // 16 bytes, needed for an IPv6 address 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 KSocketAddress 00413 { 00414 public: 00420 KSocketAddress(); 00421 00429 KSocketAddress(const sockaddr* sa, Q_UINT16 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, Q_UINT16 len); 00483 00488 inline operator const sockaddr*() const 00489 { return address(); } 00490 00494 Q_UINT16 length() const; 00495 00516 KSocketAddress& setLength(Q_UINT16 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: // static 00621 static int ianaFamily(int af); 00622 00627 static int fromIanaFamily(int iana); 00628 }; 00629 00630 00640 class KInetSocketAddress: public KSocketAddress 00641 { 00642 friend class KSocketAddress; 00643 public: 00647 KInetSocketAddress(); 00648 00658 KInetSocketAddress(const sockaddr* sa, Q_UINT16 len); 00659 00666 KInetSocketAddress(const KIpAddress& host, Q_UINT16 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 Q_UINT16 port() const; 00743 00751 KInetSocketAddress& setPort(Q_UINT16 port); 00752 00762 KInetSocketAddress& makeIPv4(); 00763 00772 KInetSocketAddress& makeIPv6(); 00773 00779 Q_UINT32 flowinfo() const; 00780 00788 KInetSocketAddress& setFlowinfo(Q_UINT32 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 * External definition 00817 */ 00818 00829 class KUnixSocketAddress: public KSocketAddress 00830 { 00831 friend class KSocketAddress; 00832 public: 00836 KUnixSocketAddress(); 00837 00846 KUnixSocketAddress(const sockaddr* sa, Q_UINT16 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 } // namespace KNetwork 00900 00901 #endif
KDE Logo
This file is part of the documentation for kdecore Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Sep 29 09:43:11 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003