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 / ksockaddr.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  18.6 KB  |  684 lines

  1. /*
  2.  *  This file is part of the KDE libraries
  3.  *  Copyright (C) 2000-2003 Thiago Macieira <thiago.macieira@kdemail.net>
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public License
  16.  *  along with this library; see the file COPYING.LIB.  If not, write to
  17.  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18.  *  Boston, MA 02110-1301, USA.
  19.  */
  20. #ifndef KSOCKADDR_H
  21. #define KSOCKADDR_H
  22.  
  23. #include <qobject.h>
  24. #include <qcstring.h>
  25. #include <qstring.h>
  26. #include "kdelibs_export.h"
  27.  
  28. /*
  29.  * This file defines a class that envelopes most, if not all, socket addresses
  30.  */
  31. typedef unsigned ksocklen_t;
  32.  
  33. struct sockaddr;
  34.  
  35. class KExtendedSocket;        // No need to define it fully
  36.  
  37. class KSocketAddressPrivate;
  38. /**
  39.  * A socket address.
  40.  *
  41.  * This class envelopes almost if not all socket addresses.
  42.  *
  43.  * @author Thiago Macieira <thiago.macieira@kdemail.net>
  44.  * @short a socket address.
  45.  */
  46. class KDECORE_EXPORT KSocketAddress: public QObject
  47. {
  48.   Q_OBJECT
  49. protected:
  50.   /**
  51.    * Creates an empty class
  52.    */
  53.   KSocketAddress() { init(); }
  54.  
  55.   /**
  56.    * Creates with given data
  57.    * @param sa a sockaddr structure
  58.    * @param size the size of @p sa
  59.    */
  60.   KSocketAddress(const sockaddr* sa, ksocklen_t size);
  61.  
  62. public:
  63.   /**
  64.    * Destructor.
  65.    */
  66.   virtual ~KSocketAddress();
  67.  
  68.   /**
  69.    * Returns a string representation of this socket.
  70.    * @return a pretty string representation
  71.    */
  72.   virtual QString pretty() const;
  73.  
  74.   /**
  75.    * Returns a sockaddr structure, for passing down to library functions.
  76.    * @return the sockaddr structure, can be 0
  77.    */
  78.   const sockaddr* address() const
  79.   { return data; }
  80.  
  81.   /**
  82.    * Returns sockaddr structure size.
  83.    * @return the size of the sockaddr structre, 0 if there is none.
  84.    */
  85.   virtual ksocklen_t size() const
  86.   { return datasize; }
  87.  
  88.   /**
  89.    * Returns a sockaddr structure, for passing down to library functions.
  90.    * @return the sockaddr structure, can be 0.
  91.    * @see address()
  92.    */
  93.   operator const sockaddr*() const
  94.   { return data; }
  95.  
  96.   /**
  97.    * Returns the family of this address.
  98.    * @return the family of this address, AF_UNSPEC if it's undefined
  99.    */
  100.   int family() const;
  101.  
  102.   /**
  103.    * Returns the IANA family number of this address.
  104.    * @return the IANA family number of this address (1 for AF_INET.
  105.    *         2 for AF_INET6, otherwise 0)
  106.    */
  107.   inline int ianaFamily() const
  108.   { return ianaFamily(family()); }
  109.  
  110.   /**
  111.    * Returns true if this equals the other socket.
  112.    * @param other    the other socket
  113.    * @return true if both sockets are equal
  114.    */
  115.   virtual bool isEqual(const KSocketAddress& other) const;
  116.   bool isEqual(const KSocketAddress* other) const
  117.   { return isEqual(*other); }
  118.  
  119.   /**
  120.    * Overloaded == operator.
  121.    * @see isEqual()
  122.    */
  123.   bool operator==(const KSocketAddress& other) const
  124.   { return isEqual(other); }
  125.  
  126.   /**
  127.    * Some sockets may differ in such things as services or port numbers,
  128.    * like Internet sockets. This function compares only the core part
  129.    * of that, if possible.
  130.    *
  131.    * If not possible, like the default implementation, this returns
  132.    * the same as isEqual.
  133.    * @param other    the other socket
  134.    * @return true if the code part is equal
  135.    */
  136.   bool isCoreEqual(const KSocketAddress& other) const;
  137.  
  138.   /**
  139.    * Some sockets may differ in such things as services or port numbers,
  140.    * like Internet sockets. This function compares only the core part
  141.    * of that, if possible.
  142.    *
  143.    * If not possible, like the default implementation, this returns
  144.    * the same as isEqual.
  145.    * @param other    the other socket
  146.    * @return true if the code part is equal
  147.    */
  148.   bool isCoreEqual(const KSocketAddress* other) const
  149.   { return isCoreEqual(*other); }
  150.  
  151.   /**
  152.    * Returns the node name of this socket, as KExtendedSocket::lookup expects
  153.    * as the first argument.
  154.    * In the case of Internet sockets, this is the hostname.
  155.    * The default implementation returns QString::null.
  156.    * @return the node name, can be QString::null
  157.    */
  158.   virtual QString nodeName() const;
  159.  
  160.   /**
  161.    * Returns the service name for this socket, as KExtendedSocket::lookup expects
  162.    * as the service argument.
  163.    * In the case of Internet sockets, this is the port number.
  164.    * The default implementation returns QString::null.
  165.    * @return the service name, can be QString::null
  166.    */
  167.   virtual QString serviceName() const;
  168.  
  169. protected:
  170.   sockaddr*    data;
  171.   ksocklen_t    datasize;
  172.   bool        owndata;
  173.  
  174. private:
  175.   void init();
  176.   /* No copy constructor */
  177.   KSocketAddress(KSocketAddress&);
  178.   KSocketAddress& operator=(KSocketAddress&);
  179.  
  180. public:
  181.   /**
  182.    * Creates a new KSocketAddress or descendant class from given
  183.    * raw socket address.
  184.    * @param sa        new socket address
  185.    * @param size    new socket address's length
  186.    * @return the new KSocketAddress, or 0 if the function failed
  187.    */
  188.   static KSocketAddress* newAddress(const struct sockaddr *sa, ksocklen_t size);
  189.  
  190.   /**
  191.    * Returns the IANA family number of the given address family.
  192.    * Returns 0 if there is no corresponding IANA family number.
  193.    * @param af        the address family, in AF_* constants
  194.    * @return the IANA family number of this address (1 for AF_INET.
  195.    *         2 for AF_INET6, otherwise 0)
  196.    */
  197.   static int ianaFamily(int af);
  198.  
  199.   /**
  200.    * Returns the address family of the given IANA family number.
  201.    * @return the address family, AF_UNSPEC for unknown IANA family numbers
  202.    */
  203.   static int fromIanaFamily(int iana);
  204.  
  205.   friend class KExtendedSocket;
  206. protected:
  207.   virtual void virtual_hook( int id, void* data );
  208. private:
  209.   KSocketAddressPrivate* d;
  210. };
  211.  
  212. /*
  213.  * External definitions
  214.  * We need these for KInetSocketAddress
  215.  */
  216. struct sockaddr_in;
  217. struct sockaddr_in6;
  218. struct in_addr;
  219. struct in6_addr;
  220.  
  221. class KInetSocketAddressPrivate;
  222. /**
  223.  * An Inet (IPv4 or IPv6) socket address
  224.  *
  225.  * This is an IPv4 or IPv6 address of the Internet
  226.  *
  227.  * This class inherits most of the functionality from KSocketAddress, but
  228.  * is targeted specifically to Internet addresses
  229.  *
  230.  * @author Thiago Macieira <thiago.macieira@kdemail.net>
  231.  * @short an Internet socket address
  232.  */
  233. class KDECORE_EXPORT KInetSocketAddress: public ::KSocketAddress
  234. {
  235.   Q_OBJECT
  236. public:
  237.   /**
  238.    * Default constructor. Does nothing
  239.    */
  240.   KInetSocketAddress();
  241.  
  242.   /**
  243.    * Copy constructor
  244.    */
  245.   KInetSocketAddress(const KInetSocketAddress&);
  246.  
  247.   /**
  248.    * Creates an IPv4 socket from raw sockaddr_in.
  249.    * @param sin        a sockaddr_in structure to copy from
  250.    * @param len        the socket address length
  251.    */
  252.   KInetSocketAddress(const sockaddr_in* sin, ksocklen_t len);
  253.  
  254.   /**
  255.    * Creates an IPv6 socket from raw sockaddr_in6.
  256.    * @param sin6           a sockaddr_in6 structure to copy from
  257.    * @param len        the socket address length
  258.    */
  259.   KInetSocketAddress(const sockaddr_in6* sin6, ksocklen_t len);
  260.  
  261.   /**
  262.    * Creates a socket from information.
  263.    * @param addr    a binary address
  264.    * @param port    a port number
  265.    */
  266.   KInetSocketAddress(const in_addr& addr, unsigned short port);
  267.  
  268.   /**
  269.    * Creates a socket from information.
  270.    * @param addr    a binary address
  271.    * @param port    a port number
  272.    */
  273.   KInetSocketAddress(const in6_addr& addr, unsigned short port);
  274.  
  275.   /**
  276.    * Creates a socket from text representation.
  277.    *
  278.    * @param addr    a text representation of the address
  279.    * @param port    a port number
  280.    * @param family    the family for this address. Use -1 to guess the
  281.    *                    family type
  282.    * @see setAddress
  283.    */
  284.   KInetSocketAddress(const QString& addr, unsigned short port, int family = -1);
  285.  
  286.   /**
  287.    * Destructor
  288.    */
  289.   virtual ~KInetSocketAddress();
  290.  
  291.   /**
  292.    * Sets this socket to given socket.
  293.    * @param ksa        the other socket
  294.    * @return true if successful, false otherwise
  295.    */
  296.   bool setAddress(const KInetSocketAddress& ksa);
  297.  
  298.   /**
  299.    * Sets this socket to given raw socket.
  300.    * @param sin        the raw socket
  301.    * @param len        the socket address length
  302.    * @return true if successful, false otherwise
  303.    */
  304.   bool setAddress(const sockaddr_in* sin, ksocklen_t len);
  305.  
  306.   /**
  307.    * Sets this socket to given raw socket.
  308.    *
  309.    * Note: this function does not clear the scope ID and flow info values
  310.    * @param sin6    the raw socket
  311.    * @param len        the socket address length
  312.    * @return true if successful, false otherwise
  313.    */
  314.   bool setAddress(const sockaddr_in6* sin6, ksocklen_t len);
  315.  
  316.   /**
  317.    * Sets this socket to raw address and port.
  318.    * @param addr    the address
  319.    * @param port    the port number
  320.    * @return true if successful, false otherwise
  321.    */
  322.   bool setAddress(const in_addr& addr, unsigned short port);
  323.  
  324.   /**
  325.    * Sets this socket to raw address and port.
  326.    * @param addr    the address
  327.    * @param port    the port number
  328.    * @return true if successful, false otherwise
  329.    */
  330.   bool setAddress(const in6_addr& addr, unsigned short port);
  331.  
  332.   /**
  333.    * Sets this socket to text address and port
  334.    *
  335.    * You can use the @p family parameter to specify what kind of socket
  336.    * you want this to be. It could be AF_INET or AF_INET6 or -1.
  337.    *
  338.    * If the value is -1 (default), this function will make an effort to
  339.    * discover what is the family. That isn't too hard, actually, and it
  340.    * works in all cases. But, if you want to be sure that your socket
  341.    * is of the type you want, use this parameter.
  342.    *
  343.    * This function returns false if the socket address was not valid.
  344.    * @param addr    the address
  345.    * @param port    the port number
  346.    * @param family    the address family, -1 for any
  347.    * @return true if successful, false otherwise
  348.    */
  349.   bool setAddress(const QString& addr, unsigned short port, int family = -1);
  350.  
  351.   /**
  352.    * Sets this socket's host address to given raw address.
  353.    * @param addr    the address
  354.    * @return true if successful, false otherwise
  355.    */
  356.   bool setHost(const in_addr& addr);
  357.  
  358.   /**
  359.    * Sets this socket's host address to given raw address.
  360.    * @param addr    the address
  361.    * @return true if successful, false otherwise
  362.    */
  363.   bool setHost(const in6_addr& addr);
  364.  
  365.   /**
  366.    * Sets this socket's host address to given text representation.
  367.    * @param addr    the address
  368.    * @param family    the address family, -1 to guess the family
  369.    * @return true if successful, false otherwise
  370.    */
  371.   bool setHost(const QString& addr, int family = -1);
  372.  
  373.   /**
  374.    * Sets this socket's port number to given port number.
  375.    * @param port    the port number
  376.    * @return true if successful, false otherwise
  377.    */
  378.   bool setPort(unsigned short port);
  379.  
  380.   /**
  381.    * Turns this into an IPv4 or IPv6 address.
  382.    *
  383.    * @param family the new address family
  384.    * @return false if this is v6 and information was lost. That doesn't
  385.    * mean the conversion was unsuccessful.
  386.    */
  387.   bool setFamily(int family);
  388.  
  389.   /**
  390.    * Sets flowinfo information for this socket address if this is IPv6.
  391.    * @param flowinfo    flowinfo
  392.    * @return true if successful, false otherwise
  393.    */
  394.   bool setFlowinfo(Q_UINT32 flowinfo);
  395.  
  396.   /**
  397.    * Sets the scope id for this socket if this is IPv6.
  398.    * @param scopeid    the scope id
  399.    * @return true if successful, false otherwise
  400.    */
  401.   bool setScopeId(int scopeid);
  402.  
  403.   /**
  404.    * Returns a pretty representation of this address.
  405.    * @return a pretty representation
  406.    */
  407.   virtual QString pretty() const;
  408.  
  409.   /**
  410.    * Returns the text representation of the host address.
  411.    * @return a text representation of the host address
  412.    */
  413.   virtual QString nodeName() const;
  414.   //  QString prettyHost() const;
  415.  
  416.   /**
  417.    * Returns the text representation of the port number.
  418.    * @return the name of the service (a number)
  419.    */
  420.   virtual QString serviceName() const;
  421.  
  422.   /**
  423.    * Returns the socket address.
  424.    *
  425.    * This will be NULL if this is a non-convertible v6.
  426.    * This function will return an IPv4 socket if this IPv6
  427.    * socket is a v4-mapped address. That is, if it's really
  428.    * an IPv4 address, but in v6 disguise.
  429.    * @return the sockaddr_in struct, can be 0.
  430.    */
  431.   const sockaddr_in* addressV4() const;
  432.  
  433.   /**
  434.    * Returns the socket address in IPv6
  435.    * @return the sockaddr_in struct, can be 0 if IPv6 is unsupported.
  436.    */
  437.   const sockaddr_in6* addressV6() const;
  438.  
  439.   /**
  440.    * Returns the host address.
  441.    * Might be empty.
  442.    * @return the host address
  443.    */
  444.   in_addr hostV4() const;
  445.  
  446.   /**
  447.    * Returns the host address.
  448.    *
  449.    * WARNING: this function is not defined if there is no IPv6 support
  450.    * @return the host address
  451.    */
  452.   in6_addr hostV6() const;
  453.  
  454.   /**
  455.    * Returns the port number.
  456.    * @return the port number
  457.    */
  458.   unsigned short port() const;
  459.  
  460.   /**
  461.    * Returns flowinfo for IPv6 socket.
  462.    * @return the flowinfo, 0 if unsupported
  463.    */
  464.   Q_UINT32 flowinfo() const;
  465.  
  466.   /**
  467.    * Returns the scope id for this IPv6 socket.
  468.    * @return the scope id
  469.    */
  470.   int scopeId() const;
  471.  
  472.   /**
  473.    * Returns the socket length.
  474.    * Will be either sizeof(sockaddr_in) or sizeof(sockaddr_in6)
  475.    * @return the length of the socket
  476.    */
  477.   virtual ksocklen_t size() const; // should be socklen_t
  478.  
  479.   /* comparation */
  480.   /**
  481.    * Compares two IPv4 addresses.
  482.    * @param s1 the first address to compare
  483.    * @param s2 the second address to compare
  484.    * @param coreOnly true if only core parts should be compared (only
  485.    *                 the address)
  486.    * @return true if the given addresses are equal.
  487.    * @see areEqualInet6()
  488.    * @see KSocketAddress::isEqual()
  489.    * @see KSocketAddress::isCoreEqual()
  490.    */
  491.   static bool areEqualInet(const KSocketAddress &s1, const KSocketAddress &s2, bool coreOnly);
  492.  
  493.   /**
  494.    * Compares two IPv6 addresses.
  495.    * @param s1 the first address to compare
  496.    * @param s2 the second address to compare
  497.    * @param coreOnly true if only core parts should be compared (only
  498.    *                 the address)
  499.    * @return true if the given addresses are equal.
  500.    * @see areEqualInet()
  501.    * @see KSocketAddress::isEqual()
  502.    * @see KSocketAddress::isCoreEqual()
  503.    */
  504.   static bool areEqualInet6(const KSocketAddress &s1, const KSocketAddress &s2, bool coreOnly);
  505.  
  506.   /* operators */
  507.  
  508.   /**
  509.    * Returns the socket address.
  510.    * This will be NULL if this is a non-convertible v6.
  511.    * @return the sockaddr_in structure, can be 0 if v6.
  512.    * @see addressV4()
  513.    */
  514.   operator const sockaddr_in*() const
  515.   { return addressV4(); }
  516.  
  517.   /**
  518.    * Returns the socket address.
  519.    * @return the sockaddr_in structure, can be 0 if v6 is unsupported.
  520.    * @see addressV6()
  521.    */
  522.   operator const sockaddr_in6*() const
  523.   { return addressV6(); }
  524.  
  525.   /**
  526.    * Sets this object to be the same as the other.
  527.    */
  528.   KInetSocketAddress& operator=(const KInetSocketAddress &other)
  529.   { setAddress(other); return *this; }
  530.  
  531. private:
  532.  
  533.   void fromV4();
  534.   void fromV6();
  535.  
  536. public:
  537.   /**
  538.    * Convert s the given raw address into text form.
  539.    * This function returns QString::null if the address cannot be converted.
  540.    * @param family    the family of the address
  541.    * @param addr    the address, in raw form
  542.    * @return the converted address, or QString::null if not possible.
  543.    */
  544.   static QString addrToString(int family, const void *addr);
  545.  
  546.   /**
  547.    * Converts the address given in text form into raw form.
  548.    * The size of the destination buffer @p dest is supposed to be
  549.    * large enough to hold the address of the given family.
  550.    * @param family    the family of the address
  551.    * @param text    the text representation of the address
  552.    * @param dest    the destination buffer of the address
  553.    * @return true if convertion was successful.
  554.    */
  555.   static bool stringToAddr(int family, const char *text, void *dest);
  556.  
  557.   friend class KExtendedSocket;
  558. protected:
  559.   virtual void virtual_hook( int id, void* data );
  560. private:
  561.   KInetSocketAddressPrivate* d;
  562. };
  563.  
  564. extern const ::KInetSocketAddress addressAny, addressLoopback;
  565.  
  566. /*
  567.  * External definition KUnixSocketAddress
  568.  */
  569. struct sockaddr_un;
  570.  
  571. class KUnixSocketAddressPrivate;
  572. /**
  573.  * A Unix socket address
  574.  *
  575.  * This is a Unix socket address.
  576.  *
  577.  * This class expects QCString instead of QString values, which means the
  578.  * filenames should be encoded in whatever form locale/system deems necessary
  579.  * before passing down to the function
  580.  *
  581.  * @author Thiago Macieira <thiago.macieira@kdemail.net>
  582.  * @short a Unix socket address
  583.  */
  584. class KDECORE_EXPORT KUnixSocketAddress: public ::KSocketAddress
  585. {
  586.   Q_OBJECT
  587. public:
  588.   /**
  589.    * Default constructor
  590.    */
  591.   KUnixSocketAddress();
  592.  
  593.   /**
  594.    * Constructor from raw data.
  595.    * @param raw_data    raw data
  596.    * @param size    data length
  597.    */
  598.   KUnixSocketAddress(const sockaddr_un* raw_data, ksocklen_t size);
  599.  
  600.   /**
  601.    * Constructor from pathname.
  602.    * @param pathname    pathname
  603.    */
  604.   KUnixSocketAddress(QCString pathname);
  605.  
  606.   /**
  607.    * Destructor
  608.    */
  609.   virtual ~KUnixSocketAddress();
  610.  
  611.   /**
  612.    * Sets this to given sockaddr_un.
  613.    * @param socket_address socket address
  614.    * @param size    the socket length
  615.    * @return true if successful, false otherwise
  616.    */
  617.   bool setAddress(const sockaddr_un* socket_address, ksocklen_t size);
  618.  
  619.   /**
  620.    * Sets this to given pathname.
  621.    * @param path    pathname
  622.    * @return true if successful, false otherwise
  623.    */
  624.   bool setAddress(QCString path);
  625.  
  626.   /**
  627.    * Returns the pathname.
  628.    * @return the pathname, can be QCString::null if uninitialized, or
  629.    *         "" if unknown
  630.    */
  631.   QCString pathname() const;
  632.  
  633.   /**
  634.    * Returns pretty representation of this socket.
  635.    * @return a pretty text representation of the socket.
  636.    */
  637.   virtual QString pretty() const;
  638.  
  639.   /*
  640.    * Returns the path in the form of a QString.
  641.    * This can be fed into KExtendedSocket.
  642.    * @return the path (can be QString::null).
  643.    * @see pathname()
  644.    */
  645.   virtual QString serviceName() const;
  646.  
  647.   /**
  648.    * Returns raw socket address.
  649.    * @return the raw socket address (can be 0 if uninitialized)
  650.    */
  651.   const sockaddr_un* address() const;
  652.  
  653.   /**
  654.    * Returns raw socket address.
  655.    * @return the raw socket address (can be 0 if uninitialized)
  656.    * @see address()
  657.    */
  658.   operator const sockaddr_un*() const
  659.   { return address(); }
  660.  
  661.   /**
  662.    * Compares two unix socket addresses.
  663.    * @param s1 the first address to compare
  664.    * @param s2 the second address to compare
  665.    * @param coreOnly true if only core parts should be compared (currently
  666.    *                 unused)
  667.    * @return true if the given addresses are equal.
  668.    * @see KSocketAddress::isEqual()
  669.    * @see KSocketAddress::isCoreEqual()
  670.    */
  671.   static bool areEqualUnix(const KSocketAddress &s1, const KSocketAddress &s2, bool coreOnly);
  672.  
  673. private:
  674.   void init();
  675.  
  676.   friend class KExtendedSocket;
  677. protected:
  678.   virtual void virtual_hook( int id, void* data );
  679. private:
  680.   KUnixSocketAddressPrivate* d;
  681. };
  682.  
  683. #endif // KSOCKADDR_H
  684.