• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

KDECore

ksocketaddress.h

Go to the documentation of this file.
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 #include <kdelibs_export.h>
00032 
00033 struct sockaddr;
00034 struct sockaddr_in;
00035 struct sockaddr_in6;
00036 struct sockaddr_un;
00037 
00038 namespace KNetwork {
00039 
00040 class KIpAddress;
00041 class KSocketAddress;
00042 class KInetSocketAddress;
00043 class KUnixSocketAddress;
00044 
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(Q_UINT32 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 
00235   inline Q_UINT32 IPv4Addr(bool convertMapped = true) const
00236   {
00237     return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0];
00238   }
00239 
00248   Q_UINT32 hostIPv4Addr(bool convertMapped = true) const;
00249 
00250 public:
00251   /*-- tests --*/
00252 
00256   inline bool isUnspecified() const
00257   { return version() == 0 ? true : (*this == anyhostV4 || *this == anyhostV6); }
00258 
00262   inline bool isLocalhost() const
00263   { return version() == 0 ? false : (*this == localhostV4 || *this == localhostV6); }
00264 
00268   inline bool isLoopback() const
00269   { return isLocalhost(); }
00270 
00277   inline bool isClassA() const
00278   { return version() != 4 ? false : (hostIPv4Addr() & 0x80000000) == 0; }
00279 
00286   inline bool isClassB() const
00287   { return version() != 4 ? false : (hostIPv4Addr() & 0xc0000000) == 0x80000000; }
00288 
00295   inline bool isClassC() const
00296   { return version() != 4 ? false : (hostIPv4Addr() & 0xe0000000) == 0xc0000000; }
00297 
00304   inline bool isClassD() const
00305   { return version() != 4 ? false : (hostIPv4Addr() & 0xf0000000) == 0xe0000000; }
00306 
00310   inline bool isMulticast() const
00311   {
00312     if (version() == 4) return isClassD();
00313     if (version() == 6) return ((Q_UINT8*)addr())[0] == 0xff;
00314     return false;
00315   }
00316 
00320   inline bool isLinkLocal() 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) == 0x80;
00326   }
00327 
00331   inline bool isSiteLocal() const
00332   {
00333     if (version() != 6) return false;
00334     Q_UINT8* addr = (Q_UINT8*)this->addr();
00335     return (addr[0] & 0xff) == 0xfe &&
00336       (addr[1] & 0xc0) == 0xc0;
00337   }
00338 
00342   inline bool isGlobal() const
00343   { return version() != 6 ? false : !(isMulticast() || isLinkLocal() || isSiteLocal()); }
00344 
00348   inline bool isV4Mapped() const
00349   {
00350     if (version() != 6) return false;
00351     Q_UINT32* addr = (Q_UINT32*)this->addr();
00352     return addr[0] == 0 && addr[1] == 0 &&
00353       ((Q_UINT16*)&addr[2])[0] == 0 &&
00354       ((Q_UINT16*)&addr[2])[1] == 0xffff;
00355   }
00356 
00360   inline bool isV4Compat() const
00361   {
00362     if (version() != 6 || isLocalhost()) return false;
00363     Q_UINT32* addr = (Q_UINT32*)this->addr();
00364     return addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] != 0;
00365   }
00366 
00370   inline bool isMulticastNodeLocal() const
00371   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x1; }
00372 
00376   inline bool isMulticastLinkLocal() const
00377   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x2; }
00378       
00382   inline bool isMulticastSiteLocal() const
00383   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x5; }
00384 
00388   inline bool isMulticastOrgLocal() const
00389   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x8; }
00390 
00394   inline bool isMulticastGlobal() const
00395   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0xe; }
00396 
00397 protected:
00398   Q_UINT32 m_data[4];          // 16 bytes, needed for an IPv6 address
00399 
00400   char m_version;
00401 
00402 public:
00404   static const KIpAddress localhostV4;
00406   static const KIpAddress anyhostV4;
00407 
00409   static const KIpAddress localhostV6;
00411   static const KIpAddress anyhostV6;
00412 };
00413 
00414 
00415 class KSocketAddressData;
00423 class KDECORE_EXPORT KSocketAddress
00424 {
00425 public:
00431   KSocketAddress();
00432 
00440   KSocketAddress(const sockaddr* sa, Q_UINT16 len);
00441 
00450   KSocketAddress(const KSocketAddress& other);
00451 
00455   virtual ~KSocketAddress();
00456 
00463   KSocketAddress& operator =(const KSocketAddress& other);
00464 
00472   const sockaddr* address() const;
00473 
00484   sockaddr* address();
00485 
00493   KSocketAddress& setAddress(const sockaddr *sa, Q_UINT16 len);
00494 
00499   inline operator const sockaddr*() const
00500   { return address(); }
00501 
00505   Q_UINT16 length() const;
00506 
00527   KSocketAddress& setLength(Q_UINT16 len);
00528 
00533   int family() const;
00534 
00543   virtual KSocketAddress& setFamily(int family);
00544 
00550   inline int ianaFamily() const
00551   { return ianaFamily(family()); }
00552   
00561   bool operator ==(const KSocketAddress& other) const;
00562 
00572   virtual QString nodeName() const;
00573 
00583   virtual QString serviceName() const;
00584 
00591   virtual QString toString() const;
00592 
00597   KInetSocketAddress& asInet();
00598 
00602   KInetSocketAddress asInet() const;
00603 
00608   KUnixSocketAddress& asUnix();
00609 
00613   KUnixSocketAddress asUnix() const;
00614 
00615 protected:
00618   KSocketAddressData *d;
00619 
00622   KSocketAddress(KSocketAddressData* d);
00623 
00624 public:             // static
00632   static int ianaFamily(int af);
00633 
00638   static int fromIanaFamily(int iana);
00639 };
00640 
00641 
00651 class KDECORE_EXPORT KInetSocketAddress: public KSocketAddress
00652 {
00653   friend class KSocketAddress;
00654 public:
00658   KInetSocketAddress();
00659 
00669   KInetSocketAddress(const sockaddr* sa, Q_UINT16 len);
00670 
00677   KInetSocketAddress(const KIpAddress& host, Q_UINT16 port);
00678 
00686   KInetSocketAddress(const KInetSocketAddress& other);
00687 
00696   KInetSocketAddress(const KSocketAddress& other);
00697 
00701   virtual ~KInetSocketAddress();
00702 
00710   KInetSocketAddress& operator =(const KInetSocketAddress& other);
00711 
00715   inline operator const sockaddr_in*() const
00716   { return (const sockaddr_in*)address(); }
00717 
00721   inline operator const sockaddr_in6*() const
00722   { return (const sockaddr_in6*)address(); }
00723 
00729   int ipVersion() const;
00730 
00734   KIpAddress ipAddress() const;
00735 
00745   KInetSocketAddress& setHost(const KIpAddress& addr);
00746 
00753   Q_UINT16 port() const;
00754 
00762   KInetSocketAddress& setPort(Q_UINT16 port);
00763 
00773   KInetSocketAddress& makeIPv4();
00774 
00783   KInetSocketAddress& makeIPv6();
00784 
00790   Q_UINT32 flowinfo() const;
00791 
00799   KInetSocketAddress& setFlowinfo(Q_UINT32 flowinfo);
00800 
00806   int scopeId() const;
00807 
00815   KInetSocketAddress& setScopeId(int scopeid);
00816 
00817 protected:
00820   KInetSocketAddress(KSocketAddressData* d);
00821 
00822 private:
00823   void update();
00824 };
00825 
00826 /*
00827  * External definition
00828  */
00829 
00840 class KDECORE_EXPORT KUnixSocketAddress: public KSocketAddress
00841 {
00842   friend class KSocketAddress;
00843 public:
00847   KUnixSocketAddress();
00848 
00857   KUnixSocketAddress(const sockaddr* sa, Q_UINT16 len);
00858 
00865   KUnixSocketAddress(const KUnixSocketAddress& other);
00866 
00870   KUnixSocketAddress(const QString& pathname);
00871 
00875   virtual ~KUnixSocketAddress();
00876 
00883   KUnixSocketAddress& operator =(const KUnixSocketAddress& other);
00884 
00888   inline operator const sockaddr_un*() const
00889   { return (const sockaddr_un*)address(); }
00890 
00895   QString pathname() const;
00896 
00902   KUnixSocketAddress& setPathname(const QString& path);
00903 
00904 protected:
00907   KUnixSocketAddress(KSocketAddressData* d);
00908 };
00909 
00910 }               // namespace KNetwork
00911 
00912 #endif

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal