# kdecore Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# 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 #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
    00236   inline Q_UINT32 IPv4Addr(bool convertMapped = true) const
    00237   {
    00238     return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0];
    00239   }
    00240
    00241   /*-- tests --*/
    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 ((Q_UINT8*)addr())[0] == 0xff;
    00304     return false;
    00305   }
    00306
    00310   inline bool isLinkLocal() const
    00311   {
    00312     if (version() != 6) return false;
    00313     Q_UINT8* addr = (Q_UINT8*)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     Q_UINT8* addr = (Q_UINT8*)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     Q_UINT32* addr = (Q_UINT32*)this->addr();
    00342     return addr[0] == 0 && addr[1] == 0 &&
    00343       ((Q_UINT16*)&addr[2])[0] == 0 &&
    00344       ((Q_UINT16*)&addr[2])[1] == 0xffff;
    00345   }
    00346
    00350   inline bool isV4Compat() const
    00351   {
    00352     if (version() != 6 || isLocalhost()) return false;
    00353     Q_UINT32* addr = (Q_UINT32*)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() && (((Q_UINT32*)addr())[0] & 0xf) == 0x1; }
    00362
    00366   inline bool isMulticastLinkLocal() const
    00367   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x2; }
    00368
    00372   inline bool isMulticastSiteLocal() const
    00373   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x5; }
    00374
    00378   inline bool isMulticastOrgLocal() const
    00379   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x8; }
    00380
    00384   inline bool isMulticastGlobal() const
    00385   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0xe; }
    00386
    00387 protected:
    00388   Q_UINT32 m_data[4];          // 16 bytes, needed for an IPv6 address
    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;
    00413 class KDECORE_EXPORT KSocketAddress
    00414 {
    00415 public:
    00421   KSocketAddress();
    00422
    00430   KSocketAddress(const sockaddr* sa, Q_UINT16 len);
    00431
    00440   KSocketAddress(const KSocketAddress& other);
    00441
    00445   virtual ~KSocketAddress();
    00446
    00453   KSocketAddress& operator =(const KSocketAddress& other);
    00454
    00462   const sockaddr* address() const;
    00463
    00474   sockaddr* address();
    00475
    00483   KSocketAddress& setAddress(const sockaddr *sa, Q_UINT16 len);
    00484
    00489   inline operator const sockaddr*() const
    00490   { return address(); }
    00491
    00495   Q_UINT16 length() const;
    00496
    00517   KSocketAddress& setLength(Q_UINT16 len);
    00518
    00523   int family() const;
    00524
    00533   virtual KSocketAddress& setFamily(int family);
    00534
    00540   inline int ianaFamily() const
    00541   { return ianaFamily(family()); }
    00542
    00551   bool operator ==(const KSocketAddress& other) const;
    00552
    00562   virtual QString nodeName() const;
    00563
    00573   virtual QString serviceName() const;
    00574
    00581   virtual QString toString() const;
    00582
    00587   KInetSocketAddress& asInet();
    00588
    00592   KInetSocketAddress asInet() const;
    00593
    00598   KUnixSocketAddress& asUnix();
    00599
    00603   KUnixSocketAddress asUnix() const;
    00604
    00605 protected:
    00608   KSocketAddressData *d;
    00609
    00612   KSocketAddress(KSocketAddressData* d);
    00613
    00614 public:             // static
    00622   static int ianaFamily(int af);
    00623
    00628   static int fromIanaFamily(int iana);
    00629 };
    00630
    00631
    00641 class KDECORE_EXPORT KInetSocketAddress: public KSocketAddress
    00642 {
    00643   friend class KSocketAddress;
    00644 public:
    00648   KInetSocketAddress();
    00649
    00659   KInetSocketAddress(const sockaddr* sa, Q_UINT16 len);
    00660
    00667   KInetSocketAddress(const KIpAddress& host, Q_UINT16 port);
    00668
    00676   KInetSocketAddress(const KInetSocketAddress& other);
    00677
    00686   KInetSocketAddress(const KSocketAddress& other);
    00687
    00691   virtual ~KInetSocketAddress();
    00692
    00700   KInetSocketAddress& operator =(const KInetSocketAddress& other);
    00701
    00705   inline operator const sockaddr_in*() const
    00706   { return (const sockaddr_in*)address(); }
    00707
    00711   inline operator const sockaddr_in6*() const
    00712   { return (const sockaddr_in6*)address(); }
    00713
    00719   int ipVersion() const;
    00720
    00724   KIpAddress ipAddress() const;
    00725
    00735   KInetSocketAddress& setHost(const KIpAddress& addr);
    00736
    00743   Q_UINT16 port() const;
    00744
    00752   KInetSocketAddress& setPort(Q_UINT16 port);
    00753
    00763   KInetSocketAddress& makeIPv4();
    00764
    00773   KInetSocketAddress& makeIPv6();
    00774
    00780   Q_UINT32 flowinfo() const;
    00781
    00789   KInetSocketAddress& setFlowinfo(Q_UINT32 flowinfo);
    00790
    00796   int scopeId() const;
    00797
    00805   KInetSocketAddress& setScopeId(int scopeid);
    00806
    00807 protected:
    00810   KInetSocketAddress(KSocketAddressData* d);
    00811
    00812 private:
    00813   void update();
    00814 };
    00815
    00816 /*
    00817  * External definition
    00818  */
    00819
    00830 class KDECORE_EXPORT KUnixSocketAddress: public KSocketAddress
    00831 {
    00832   friend class KSocketAddress;
    00833 public:
    00837   KUnixSocketAddress();
    00838
    00847   KUnixSocketAddress(const sockaddr* sa, Q_UINT16 len);
    00848
    00855   KUnixSocketAddress(const KUnixSocketAddress& other);
    00856
    00860   KUnixSocketAddress(const QString& pathname);
    00861
    00865   virtual ~KUnixSocketAddress();
    00866
    00873   KUnixSocketAddress& operator =(const KUnixSocketAddress& other);
    00874
    00878   inline operator const sockaddr_un*() const
    00879   { return (const sockaddr_un*)address(); }
    00880
    00885   QString pathname() const;
    00886
    00892   KUnixSocketAddress& setPathname(const QString& path);
    00893
    00894 protected:
    00897   KUnixSocketAddress(KSocketAddressData* d);
    00898 };
    00899
    00900 }               // namespace KNetwork
    00901
    00902 #endif

This file is part of the documentation for kdecore Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:18:10 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003