home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / ksocketaddress.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-24  |  24.8 KB  |  913 lines

  1. /*  -*- C++ -*-
  2.  *  Copyright (C) 2003 Thiago Macieira <thiago.macieira@kdemail.net>
  3.  *
  4.  *
  5.  *  Permission is hereby granted, free of charge, to any person obtaining
  6.  *  a copy of this software and associated documentation files (the
  7.  *  "Software"), to deal in the Software without restriction, including
  8.  *  without limitation the rights to use, copy, modify, merge, publish,
  9.  *  distribute, sublicense, and/or sell copies of the Software, and to
  10.  *  permit persons to whom the Software is furnished to do so, subject to
  11.  *  the following conditions:
  12.  *
  13.  *  The above copyright notice and this permission notice shall be included 
  14.  *  in all copies or substantial portions of the Software.
  15.  *
  16.  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17.  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18.  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19.  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20.  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21.  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22.  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  */
  24.  
  25. #ifndef KSOCKETADDRESS_H
  26. #define KSOCKETADDRESS_H
  27.  
  28. #include <qstring.h>
  29. #include <qcstring.h>
  30.  
  31. #include <kdelibs_export.h>
  32.  
  33. struct sockaddr;
  34. struct sockaddr_in;
  35. struct sockaddr_in6;
  36. struct sockaddr_un;
  37.  
  38. namespace KNetwork {
  39.  
  40. class KIpAddress;
  41. class KSocketAddress;
  42. class KInetSocketAddress;
  43. class KUnixSocketAddress;
  44.  
  45. /** @class KIpAddress ksocketaddress.h ksocketaddress.h
  46.  *  @brief An IP address.
  47.  *
  48.  * This class represents one IP address, version 4 or 6. This is only
  49.  * the address, not including port information or other data.
  50.  *
  51.  * It is not a good programming practice to create address from objects
  52.  * like this. Instead, prefer a more thorough function like
  53.  * @ref KResolver::resolve, which also handle extra information like scope
  54.  * ids.
  55.  *
  56.  * This is a light-weight class. Most of the member functions are inlined and
  57.  * there are no virtual functions. This object's size should be less than 20
  58.  * bytes. Also note that there is no sharing of data.
  59.  *
  60.  * @author Thiago Macieira <thiago.macieira@kdemail.net>
  61.  */
  62. class KDECORE_EXPORT KIpAddress
  63. {
  64. public:
  65.   /**
  66.    * Default constructor. Creates an empty address.
  67.    * It defaults to IP version 4.
  68.    */
  69.   inline KIpAddress() : m_version(0)
  70.   { }
  71.  
  72.   /**
  73.    * Copy constructor. Copies the data from the other
  74.    * object.
  75.    *
  76.    * Data is not shared.
  77.    *
  78.    * @param other        the other
  79.    */
  80.   inline KIpAddress(const KIpAddress& other)
  81.   { *this = other; }
  82.  
  83.   /**
  84.    * Creates an object from the given string representation.
  85.    *
  86.    * The IP version is guessed from the address format.
  87.    *
  88.    * @param addr        the address
  89.    */
  90.   inline KIpAddress(const QString& addr)
  91.   { setAddress(addr); }
  92.  
  93.   /**
  94.    * Creates an object from the given string representation.
  95.    *
  96.    * The IP version is guessed from the address format.
  97.    *
  98.    * @param addr        the address
  99.    */
  100.   inline KIpAddress(const char* addr)
  101.   { setAddress(addr); }
  102.  
  103.   /**
  104.    * Creates an object from the given raw data and IP version.
  105.    *
  106.    * @param addr        the raw data
  107.    * @param version        the IP version (4 or 6)
  108.    */
  109.   inline KIpAddress(const void* addr, int version = 4)
  110.   { setAddress(addr, version); }
  111.  
  112.   /**
  113.    * This is a convenience constructor. Constructs an object
  114.    * from the given IPv4 address in the form of an integer.
  115.    *
  116.    * Note: do not write code to depend on IPv4 addresses being
  117.    * integer types. Instead, treat them as a special type, like
  118.    * a KIpAddress or the system's in_addr.
  119.    *
  120.    * @param ip4addr        the IPv4 address
  121.    */
  122.   inline KIpAddress(Q_UINT32 ip4addr)
  123.   { setAddress(&ip4addr, 4); }
  124.  
  125.   /**
  126.    * Destructor. This frees resources associated with this object.
  127.    *
  128.    * Note: destructor is non-virtual. The compiler will happily optimise it
  129.    * out of the way.
  130.    */
  131.   inline ~KIpAddress()
  132.   { }
  133.  
  134.   /**
  135.    * Copy operator.
  136.    *
  137.    * Copies the data from the other object into this one.
  138.    *
  139.    * @param other        the object to copy
  140.    */
  141.   KIpAddress& operator =(const KIpAddress& other);
  142.  
  143.   /**
  144.    * Returns true if the two addresses match.
  145.    * This function performs a v4-mapped check.
  146.    * @see compare
  147.    */
  148.   inline bool operator ==(const KIpAddress& other) const
  149.   { return compare(other, true); }
  150.  
  151.   /**
  152.    * Compares this address against the other, supplied one and return
  153.    * true if they match. The @p checkMapped parameter controls whether
  154.    * a check for an IPv6 v4-mapped address will be performed.
  155.    *
  156.    * An IPv6 v4-mapped address is an IPv6 address that is, for all purposes,
  157.    * equivalent to an IPv4 one. The default behaviour of this function
  158.    * is to take that into account. If you want a strict matching,
  159.    * pass @b false to the @p checkMapped parameter.
  160.    *
  161.    * @param other    the other IP address
  162.    * @param checkMapped    whether v4-mapped addresses will be taken into account
  163.    */
  164.   bool compare(const KIpAddress& other, bool checkMapped = true) const;
  165.  
  166.   /**
  167.    * Retrieves the IP version in this object.
  168.    *
  169.    * @returns the version: 4 or 6
  170.    */
  171.   inline int version() const
  172.   { return m_version; }
  173.  
  174.   /**
  175.    * Returns true if this is an IPv4 address.
  176.    */
  177.   inline bool isIPv4Addr() const
  178.   { return version() == 4; }
  179.  
  180.   /**
  181.    * Returns true if this is an IPv6 address.
  182.    */
  183.   inline bool isIPv6Addr() const
  184.   { return version() == 6; }
  185.  
  186.   /**
  187.    * Sets the address to the given string representation.
  188.    *
  189.    * @return true if the address was successfully parsed; otherwise returns
  190.    * false and leaves the object unchanged.
  191.    */
  192.   bool setAddress(const QString& address);
  193.  
  194.   /**
  195.    * Sets the address to the given string representation.
  196.    *
  197.    * @return true if the address was successfully parsed; otherwise returns
  198.    * false and leaves the object unchanged.
  199.    */
  200.   bool setAddress(const char* address);
  201.  
  202.   /**
  203.    * Sets the address to the given raw binary representation.
  204.    *
  205.    * @param raw            a pointer to the raw binary data
  206.    * @param version        the IP version
  207.    * @return true if the address was successfully parsed; otherwise returns
  208.    * false and leaves the object unchanged.
  209.    */
  210.   bool setAddress(const void* raw, int version = 4);
  211.  
  212.   /**
  213.    * Returns the address as a string.
  214.    */
  215.   QString toString() const;
  216.  
  217.   /**
  218.    * Returns a pointer to binary raw data representing the address.
  219.    */
  220.   inline const void *addr() const
  221.   { return m_data; }
  222.  
  223.   /**
  224.    * This is a convenience function. Returns the IPv4 address in a
  225.    * 32-bit integer. The result is only valid if @ref isIPv4Addr returns
  226.    * true. Alternatively, if the contained IPv6 address is a v4-mapped one
  227.    * and the @p convertMapped parameter is true, the result will also be
  228.    * valid. The address returned is in network byte order.
  229.    *
  230.    * Note: you should not treat IP addresses as integers. Instead,
  231.    * use types defined for that purpose, such as KIpAddress or the
  232.    * system's in_addr type.
  233.    *
  234.    */
  235.   inline Q_UINT32 IPv4Addr(bool convertMapped = true) const
  236.   {
  237.     return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0];
  238.   }
  239.  
  240.   /**
  241.    * This is a convenience function. Returns the IPv4 address in a
  242.    * 32-bit integer. The result is only valid if @ref isIPv4Addr returns
  243.    * true. Alternatively, if the contained IPv6 address is a v4-mapped one
  244.    * and the @p convertMapped parameter is true, the result will also be
  245.    * valid. The address returned is in host byte order.
  246.    *
  247.    */
  248.   Q_UINT32 hostIPv4Addr(bool convertMapped = true) const;
  249.  
  250. public:
  251.   /*-- tests --*/
  252.  
  253.   /**
  254.    * Returns true if this is the IPv4 or IPv6 unspecified address.
  255.    */
  256.   inline bool isUnspecified() const
  257.   { return version() == 0 ? true : (*this == anyhostV4 || *this == anyhostV6); }
  258.  
  259.   /**
  260.    *  Returns true if this is either the IPv4 or the IPv6 localhost address.
  261.    */
  262.   inline bool isLocalhost() const
  263.   { return version() == 0 ? false : (*this == localhostV4 || *this == localhostV6); }
  264.  
  265.   /**
  266.    * This is an alias for @ref isLocalhost.
  267.    */
  268.   inline bool isLoopback() const
  269.   { return isLocalhost(); }
  270.  
  271.   /**
  272.    * Returns true if this is an IPv4 class A address, i.e., 
  273.    * from 0.0.0.0 to 127.255.255.255.
  274.    *
  275.    * This function does not test for v4-mapped addresses.
  276.    */
  277.   inline bool isClassA() const
  278.   { return version() != 4 ? false : (hostIPv4Addr() & 0x80000000) == 0; }
  279.  
  280.   /**
  281.    * Returns true if this is an IPv4 class B address, i.e., one from
  282.    * 128.0.0.0 to 191.255.255.255.
  283.    *
  284.    * This function does not test for v4-mapped addresses.
  285.    */
  286.   inline bool isClassB() const
  287.   { return version() != 4 ? false : (hostIPv4Addr() & 0xc0000000) == 0x80000000; }
  288.  
  289.   /**
  290.    * Returns true if this is an IPv4 class C address, i.e., one from
  291.    * 192.0.0.0 to 223.255.255.255.
  292.    *
  293.    * This function does not test for v4-mapped addresses.
  294.    */
  295.   inline bool isClassC() const
  296.   { return version() != 4 ? false : (hostIPv4Addr() & 0xe0000000) == 0xc0000000; }
  297.  
  298.   /**
  299.    * Returns true if this is an IPv4 class D (a.k.a. multicast) address.
  300.    *
  301.    * Note: this function is not the same as @ref isMulticast. isMulticast also
  302.    * tests for IPv6 multicast addresses.
  303.    */
  304.   inline bool isClassD() const
  305.   { return version() != 4 ? false : (hostIPv4Addr() & 0xf0000000) == 0xe0000000; }
  306.  
  307.   /**
  308.    * Returns true if this is a multicast address, be it IPv4 or IPv6.
  309.    */
  310.   inline bool isMulticast() const
  311.   {
  312.     if (version() == 4) return isClassD();
  313.     if (version() == 6) return ((Q_UINT8*)addr())[0] == 0xff;
  314.     return false;
  315.   }
  316.  
  317.   /**
  318.    * Returns true if this is an IPv6 link-local address.
  319.    */
  320.   inline bool isLinkLocal() const
  321.   { 
  322.     if (version() != 6) return false;
  323.     Q_UINT8* addr = (Q_UINT8*)this->addr();
  324.     return (addr[0] & 0xff) == 0xfe &&
  325.       (addr[1] & 0xc0) == 0x80;
  326.   }
  327.  
  328.   /**
  329.    * Returns true if this is an IPv6 site-local address.
  330.    */
  331.   inline bool isSiteLocal() const
  332.   {
  333.     if (version() != 6) return false;
  334.     Q_UINT8* addr = (Q_UINT8*)this->addr();
  335.     return (addr[0] & 0xff) == 0xfe &&
  336.       (addr[1] & 0xc0) == 0xc0;
  337.   }
  338.  
  339.   /**
  340.    * Returns true if this is a global IPv6 address.
  341.    */
  342.   inline bool isGlobal() const
  343.   { return version() != 6 ? false : !(isMulticast() || isLinkLocal() || isSiteLocal()); }
  344.  
  345.   /**
  346.    * Returns true if this is a v4-mapped IPv6 address.
  347.    */
  348.   inline bool isV4Mapped() const
  349.   {
  350.     if (version() != 6) return false;
  351.     Q_UINT32* addr = (Q_UINT32*)this->addr();
  352.     return addr[0] == 0 && addr[1] == 0 &&
  353.       ((Q_UINT16*)&addr[2])[0] == 0 &&
  354.       ((Q_UINT16*)&addr[2])[1] == 0xffff;
  355.   }
  356.  
  357.   /**
  358.    * Returns true if this is a v4-compat IPv6 address.
  359.    */
  360.   inline bool isV4Compat() const
  361.   {
  362.     if (version() != 6 || isLocalhost()) return false;
  363.     Q_UINT32* addr = (Q_UINT32*)this->addr();
  364.     return addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] != 0;
  365.   }
  366.  
  367.   /**
  368.    * Returns true if this is an IPv6 node-local multicast address.
  369.    */
  370.   inline bool isMulticastNodeLocal() const
  371.   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x1; }
  372.  
  373.   /**
  374.    * Returns true if this is an IPv6 link-local multicast address.
  375.    */
  376.   inline bool isMulticastLinkLocal() const
  377.   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x2; }
  378.       
  379.   /**
  380.    * Returns true if this is an IPv6 site-local multicast address.
  381.    */
  382.   inline bool isMulticastSiteLocal() const
  383.   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x5; }
  384.  
  385.   /**
  386.    * Returns true if this is an IPv6 organisational-local multicast address.
  387.    */
  388.   inline bool isMulticastOrgLocal() const
  389.   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0x8; }
  390.  
  391.   /**
  392.    * Returns true if this is an IPv6 global multicast address.
  393.    */
  394.   inline bool isMulticastGlobal() const
  395.   { return version() == 6 && isMulticast() && (((Q_UINT32*)addr())[0] & 0xf) == 0xe; }
  396.  
  397. protected:
  398.   Q_UINT32 m_data[4];           // 16 bytes, needed for an IPv6 address
  399.  
  400.   char m_version;
  401.  
  402. public:
  403.   /// localhost in IPv4 (127.0.0.1)
  404.   static const KIpAddress localhostV4;
  405.   /// the any host or undefined address in IPv4 (0.0.0.0)
  406.   static const KIpAddress anyhostV4;
  407.  
  408.   /// localhost in IPv6 (::1)
  409.   static const KIpAddress localhostV6;
  410.   /// the any host or undefined address in IPv6 (::)
  411.   static const KIpAddress anyhostV6;
  412. };
  413.  
  414.  
  415. class KSocketAddressData;
  416. /** @class KSocketAddress ksocketaddress.h ksocketaddress.h
  417.  *  @brief A generic socket address.
  418.  *
  419.  * This class holds one generic socket address.
  420.  *
  421.  * @author Thiago Macieira <thiago.macieira@kdemail.net>
  422.  */
  423. class KDECORE_EXPORT KSocketAddress
  424. {
  425. public:
  426.   /**
  427.    * Default constructor.
  428.    *
  429.    * Creates an empty object
  430.    */
  431.   KSocketAddress();
  432.  
  433.   /**
  434.    * Creates this object with the given data.
  435.    * The raw socket address is copied into this object.
  436.    *
  437.    * @param sa        the socket address structure
  438.    * @param len        the socket address length
  439.    */
  440.   KSocketAddress(const sockaddr* sa, Q_UINT16 len);
  441.  
  442.   /**
  443.    * Copy constructor. This creates a copy of the other
  444.    * object.
  445.    *
  446.    * Data is not shared.
  447.    *
  448.    * @param other    the object to copy from
  449.    */
  450.   KSocketAddress(const KSocketAddress& other);
  451.  
  452.   /**
  453.    * Destructor. Frees any associated resources.
  454.    */
  455.   virtual ~KSocketAddress();
  456.  
  457.   /**
  458.    * Performs a shallow copy of the other object into this one.
  459.    * Data will be copied.
  460.    *
  461.    * @param other    the object to copy from
  462.    */
  463.   KSocketAddress& operator =(const KSocketAddress& other);
  464.  
  465.   /**
  466.    * Returns the socket address structure, to be passed down to
  467.    * low level functions.
  468.    *
  469.    * Note that this function returns NULL for invalid or empty sockets,
  470.    * so you may use to to test for validity.
  471.    */
  472.   const sockaddr* address() const;
  473.  
  474.   /**
  475.    * Returns the socket address structure, to be passed down to
  476.    * low level functions.
  477.    *
  478.    * Note that this function returns NULL for invalid or empty sockets,
  479.    * so you may use to to test for validity.
  480.    *
  481.    * The returned value, if not NULL, is an internal buffer which is guaranteed
  482.    * to be at least @ref length() bytes long.
  483.    */
  484.   sockaddr* address();
  485.  
  486.   /**
  487.    * Sets the address to the given address.
  488.    * The raw socket address is copied into this object.
  489.    *
  490.    * @param sa        the socket address structure
  491.    * @param len        the socket address length
  492.    */
  493.   KSocketAddress& setAddress(const sockaddr *sa, Q_UINT16 len);
  494.  
  495.   /**
  496.    * Returns the socket address structure, to be passed down to
  497.    * low level functions.
  498.    */
  499.   inline operator const sockaddr*() const
  500.   { return address(); }
  501.  
  502.   /**
  503.    * Returns the length of this socket address structure.
  504.    */
  505.   Q_UINT16 length() const;
  506.  
  507.   /**
  508.    * Sets the length of this socket structure.
  509.    *
  510.    * Use this function with care. It allows you to resize the internal
  511.    * buffer to fit needs. This function should not be used except for handling
  512.    * unknown socket address structures.
  513.    *
  514.    * Also note that this function may invalidate the socket if a known
  515.    * family is set (Internet or Unix socket) and the new length would be
  516.    * too small to hold the system's sockaddr_* structure. If unsure, reset
  517.    * the family:
  518.    *
  519.    * \code
  520.    *   KSocketAddress qsa;
  521.    *   [...]
  522.    *   qsa.setFamily(AF_UNSPEC).setLength(newlen);
  523.    * \endcode
  524.    *
  525.    * @param len        the new length
  526.    */
  527.   KSocketAddress& setLength(Q_UINT16 len);
  528.  
  529.   /**
  530.    * Returns the family of this address.
  531.    * @return the family of this address, AF_UNSPEC if it's undefined
  532.    */
  533.   int family() const;
  534.  
  535.   /**
  536.    * Sets the family of this object.
  537.    *
  538.    * Note: setting the family will probably invalidate any address data
  539.    * contained in this object. Use this function with care.
  540.    *
  541.    * @param family    the new family to set
  542.    */
  543.   virtual KSocketAddress& setFamily(int family);
  544.  
  545.   /**
  546.    * Returns the IANA family number of this address.
  547.    * @return the IANA family number of this address (1 for AF_INET.
  548.    *         2 for AF_INET6, otherwise 0)
  549.    */
  550.   inline int ianaFamily() const
  551.   { return ianaFamily(family()); }
  552.   
  553.   /**
  554.    * Returns true if this equals the other socket.
  555.    *
  556.    * Socket addresses are considered matching if and only if all data is the same.
  557.    *
  558.    * @param other    the other socket
  559.    * @return true if both sockets are equal
  560.    */
  561.   bool operator ==(const KSocketAddress& other) const;
  562.  
  563.   /**
  564.    * Returns the node name of this socket.
  565.    *
  566.    * In the case of Internet sockets, this is string representation of the IP address.
  567.    * The default implementation returns QString::null.
  568.    *
  569.    * @return the node name, can be QString::null
  570.    * @bug use KResolver to resolve unknown families
  571.    */
  572.   virtual QString nodeName() const;
  573.  
  574.   /**
  575.    * Returns the service name for this socket.
  576.    *
  577.    * In the case of Internet sockets, this is the port number.
  578.    * The default implementation returns QString::null.
  579.    *
  580.    * @return the service name, can be QString::null
  581.    * @bug use KResolver to resolve unknown families
  582.    */
  583.   virtual QString serviceName() const;
  584.  
  585.   /**
  586.    * Returns this socket address as a string suitable for
  587.    * printing. Family, node and service are part of this address.
  588.    *
  589.    * @bug use KResolver to resolve unknown families
  590.    */
  591.   virtual QString toString() const;
  592.  
  593.   /**
  594.    * Returns an object reference that can be used to manipulate this socket
  595.    * as an Internet socket address. Both objects share the same data.
  596.    */
  597.   KInetSocketAddress& asInet();
  598.  
  599.   /**
  600.    * Returns an object is equal to this object's data, but they don't share it.
  601.    */
  602.   KInetSocketAddress asInet() const;
  603.  
  604.   /**
  605.    * Returns an object reference that can be used to manipulate this socket
  606.    * as a Unix socket address. Both objects share the same data.
  607.    */
  608.   KUnixSocketAddress& asUnix();
  609.  
  610.   /**
  611.    * Returns an object is equal to this object's data, but they don't share it.
  612.    */
  613.   KUnixSocketAddress asUnix() const;
  614.  
  615. protected:
  616.   /// @internal
  617.   /// private data
  618.   KSocketAddressData *d;
  619.  
  620.   /// @internal
  621.   /// extra constructor
  622.   KSocketAddress(KSocketAddressData* d);
  623.  
  624. public:                // static
  625.   /**
  626.    * Returns the IANA family number of the given address family.
  627.    * Returns 0 if there is no corresponding IANA family number.
  628.    * @param af        the address family, in AF_* constants
  629.    * @return the IANA family number of this address (1 for AF_INET.
  630.    *         2 for AF_INET6, otherwise 0)
  631.    */
  632.   static int ianaFamily(int af);
  633.  
  634.   /**
  635.    * Returns the address family of the given IANA family number.
  636.    * @return the address family, AF_UNSPEC for unknown IANA family numbers
  637.    */
  638.   static int fromIanaFamily(int iana);
  639. };
  640.  
  641.  
  642. /** @class KInetSocketAddress ksocketaddress.h ksocketaddress.h
  643.  *  @brief an Internet socket address
  644.  *
  645.  * An Inet (IPv4 or IPv6) socket address
  646.  *
  647.  * This is an IPv4 or IPv6 address of the Internet.
  648.  *
  649.  * @author Thiago Macieira <thiago.macieira@kdemail.net>
  650.  */
  651. class KDECORE_EXPORT KInetSocketAddress: public KSocketAddress
  652. {
  653.   friend class KSocketAddress;
  654. public:
  655.   /**
  656.    * Public constructor. Creates an empty object.
  657.    */
  658.   KInetSocketAddress();
  659.  
  660.   /**
  661.    * Creates an object from raw data.
  662.    *
  663.    * Note: if the socket address @p sa does not contain a valid Internet
  664.    * socket (IPv4 or IPv6), this object will be empty.
  665.    *
  666.    * @param sa        the sockaddr structure
  667.    * @param len        the structure's length
  668.    */
  669.   KInetSocketAddress(const sockaddr* sa, Q_UINT16 len);
  670.  
  671.   /**
  672.    * Creates an object from an IP address and port.
  673.    *
  674.    * @param host    the IP address
  675.    * @param port    the port number
  676.    */
  677.   KInetSocketAddress(const KIpAddress& host, Q_UINT16 port);
  678.  
  679.   /**
  680.    * Copy constructor.
  681.    *
  682.    * Data is not shared.
  683.    *
  684.    * @param other    the other object
  685.    */
  686.   KInetSocketAddress(const KInetSocketAddress& other);
  687.  
  688.   /**
  689.    * Copy constructor.
  690.    *
  691.    * If the other, generic socket address contains an Internet address,
  692.    * it will be copied. Otherwise, this object will be empty.
  693.    *
  694.    * @param other    the other object
  695.    */
  696.   KInetSocketAddress(const KSocketAddress& other);
  697.  
  698.   /**
  699.    * Destroys this object.
  700.    */
  701.   virtual ~KInetSocketAddress();
  702.  
  703.   /**
  704.    * Copy operator.
  705.    *
  706.    * Copies the other object into this one.
  707.    *
  708.    * @param other    the other object
  709.    */
  710.   KInetSocketAddress& operator =(const KInetSocketAddress& other);
  711.  
  712.   /**
  713.    * Cast operator to sockaddr_in.
  714.    */
  715.   inline operator const sockaddr_in*() const
  716.   { return (const sockaddr_in*)address(); }
  717.  
  718.   /**
  719.    * Cast operator to sockaddr_in6.
  720.    */
  721.   inline operator const sockaddr_in6*() const
  722.   { return (const sockaddr_in6*)address(); }
  723.  
  724.   /**
  725.    * Returns the IP version of the address this object holds.
  726.    *
  727.    * @return 4 or 6, if IPv4 or IPv6, respectively; 0 if this object is empty
  728.    */
  729.   int ipVersion() const;
  730.  
  731.   /**
  732.    * Returns the IP address component.
  733.    */
  734.   KIpAddress ipAddress() const;
  735.  
  736.   /**
  737.    * Sets the IP address to the given raw address.
  738.    *
  739.    * This call will preserve port numbers accross IP versions, but will lose
  740.    * IPv6 specific data if the address is set to IPv4.
  741.    *
  742.    * @param addr    the address to set to
  743.    * @return a reference to itself
  744.    */
  745.   KInetSocketAddress& setHost(const KIpAddress& addr);
  746.  
  747.   /**
  748.    * Retrieves the port number stored in this object.
  749.    *
  750.    * @return a port number in the range 0 to 65535, inclusive. An empty or 
  751.    * invalid object will have a port number of 0.
  752.    */
  753.   Q_UINT16 port() const;
  754.  
  755.   /**
  756.    * Sets the port number. If this object is empty, this function will default to
  757.    * creating an IPv4 address.
  758.    *
  759.    * @param port    the port number to set
  760.    * @return a reference to itself
  761.    */
  762.   KInetSocketAddress& setPort(Q_UINT16 port);
  763.  
  764.   /**
  765.    * Converts this object to an IPv4 socket address. It has no effect if the object
  766.    * is already an IPv4 socket address.
  767.    *
  768.    * If this object is an IPv6 address, the port number is preserved. All other information
  769.    * is lost.
  770.    *
  771.    * @return a reference to itself
  772.    */
  773.   KInetSocketAddress& makeIPv4();
  774.  
  775.   /**
  776.    * Converts this object to an IPv6 socket address. It has no effect if the object
  777.    * is already an IPv6 socket address.
  778.    *
  779.    * If this object is an IPv4 address, the port number is preserved.
  780.    *
  781.    * @return a reference to itself
  782.    */
  783.   KInetSocketAddress& makeIPv6();
  784.  
  785.   /**
  786.    * Returns the flowinfo information from the IPv6 socket address.
  787.    *
  788.    * @return the flowinfo information or 0 if this object is empty or IPv4
  789.    */
  790.   Q_UINT32 flowinfo() const;
  791.  
  792.   /**
  793.    * Sets the flowinfo information for an IPv6 socket address. If this is not
  794.    * an IPv6 socket address, this function converts it to one. See makeIPv6.
  795.    *
  796.    * @param flowinfo        the flowinfo to set
  797.    * @return a reference to itself
  798.    */
  799.   KInetSocketAddress& setFlowinfo(Q_UINT32 flowinfo);
  800.  
  801.   /**
  802.    * Returns the scope id this IPv6 socket is bound to.
  803.    *
  804.    * @return the scope id, or 0 if this is not an IPv6 object
  805.    */
  806.   int scopeId() const;
  807.  
  808.   /**
  809.    * Sets the scope id for this IPv6 object. If this is not an IPv6 socket
  810.    * address, this function converts it to one. See makeIPv6
  811.    *
  812.    * @param scopeid        the scopeid to set
  813.    * @return a reference to itself
  814.    */
  815.   KInetSocketAddress& setScopeId(int scopeid);
  816.  
  817. protected:
  818.   /// @internal
  819.   /// extra constructor
  820.   KInetSocketAddress(KSocketAddressData* d);
  821.  
  822. private:
  823.   void update();
  824. };
  825.  
  826. /*
  827.  * External definition
  828.  */
  829.  
  830. /** @class KUnixSocketAddress ksocketaddress.h ksocketaddress.h
  831.  *  @brief A Unix (local) socket address.
  832.  *
  833.  * This is a Unix socket address.
  834.  *
  835.  * Note that this class uses QStrings to represent filenames, which means
  836.  * the proper encoding is used to translate into valid filesystem file names.
  837.  *
  838.  * @author Thiago Macieira <thiago.macieira@kdemail.net>
  839.  */
  840. class KDECORE_EXPORT KUnixSocketAddress: public KSocketAddress
  841. {
  842.   friend class KSocketAddress;
  843. public:
  844.   /**
  845.    * Default constructor. Creates an empty object.
  846.    */
  847.   KUnixSocketAddress();
  848.  
  849.   /**
  850.    * Creates this object with the given raw data. If
  851.    * the sockaddr structure does not contain a Local namespace
  852.    * (Unix) socket, this object will be created empty.
  853.    *
  854.    * @param sa        the socket address structure
  855.    * @param len        the structure's length
  856.    */
  857.   KUnixSocketAddress(const sockaddr* sa, Q_UINT16 len);
  858.  
  859.   /**
  860.    * Copy constructor. Creates a copy of the other object,
  861.    * sharing the data explicitly.
  862.    *
  863.    * @param other    the other object
  864.    */
  865.   KUnixSocketAddress(const KUnixSocketAddress& other);
  866.  
  867.   /**
  868.    * Constructs an object from the given pathname.
  869.    */
  870.   KUnixSocketAddress(const QString& pathname);
  871.  
  872.   /**
  873.    * Destructor.
  874.    */
  875.   virtual ~KUnixSocketAddress();
  876.  
  877.   /**
  878.    * Copy operator. Copies the contents of the other object into
  879.    * this one. Data is explicitly shared.
  880.    *
  881.    * @param other        the other
  882.    */
  883.   KUnixSocketAddress& operator =(const KUnixSocketAddress& other);
  884.  
  885.   /**
  886.    * Cast operator to sockaddr_un.
  887.    */
  888.   inline operator const sockaddr_un*() const
  889.   { return (const sockaddr_un*)address(); }
  890.  
  891.   /**
  892.    * Returns the pathname associated with this object. Will return
  893.    * QString::null if this object is empty.
  894.    */
  895.   QString pathname() const;
  896.  
  897.   /**
  898.    * Sets the pathname for the object.
  899.    *
  900.    * @return a reference to itself
  901.    */
  902.   KUnixSocketAddress& setPathname(const QString& path);
  903.  
  904. protected:
  905.   /// @internal
  906.   /// extra constructor
  907.   KUnixSocketAddress(KSocketAddressData* d);
  908. };
  909.  
  910. }                // namespace KNetwork
  911.  
  912. #endif
  913.