BSDSocketAddress.hh

Go to the documentation of this file.
00001 // $Id: BSDSocketAddress.hh 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2008
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #ifndef HH_SENF_Socket_Protocols_BSDSocketAddress_
00027 #define HH_SENF_Socket_Protocols_BSDSocketAddress_ 1
00028 
00029 // Custom includes
00030 #include <boost/type_traits/alignment_of.hpp>
00031 #include <boost/type_traits/type_with_alignment.hpp>
00032 #include <boost/operators.hpp>
00033 #include <senf/Utils/safe_bool.hh>
00034 #include <sys/socket.h>
00035 #include <iostream>
00036 
00037 //#include "BSDSocketAddress.mpp"
00038 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00039 
00040 namespace senf {
00041 
00076     class BSDSocketAddress
00077         : public senf::comparable_safe_bool<BSDSocketAddress>,
00078           public boost::less_than_comparable<BSDSocketAddress>,
00079           public boost::equality_comparable<BSDSocketAddress>
00080     {
00081     public:
00082         bool operator==(BSDSocketAddress const & other) const; 
00083 
00086         bool operator<(BSDSocketAddress const & other) const; 
00087 
00091         bool boolean_test() const;      
00092 
00097         short family() const;           
00098 
00102         //-////////////////////////////////////////////////////////////////////////
00104         //\{
00105 
00106         struct sockaddr const * sockaddr_p() const;
00107         socklen_t socklen() const;
00108         socklen_t const * socklen_p() const;
00109 
00110         //\}
00111 
00112     protected:
00113         BSDSocketAddress(socklen_t len, short family);
00114         BSDSocketAddress(BSDSocketAddress const & other);
00115         BSDSocketAddress & operator=(BSDSocketAddress const & other);
00116 
00117         struct sockaddr * sockaddr_p();
00118         socklen_t * socklen_p();
00119 
00120         void socklen(socklen_t len);
00121 
00122     private:
00123         // The following incantation is needed to fix the alignment of the sockaddr data members
00124         // which will be added by the derived classes later: The alignment must be forced
00125         // to coincide with the struct sockaddr_storage alignment (which must have the largest
00126         // alignment of all sockaddr types).
00127         union {
00128             socklen_t len_;
00129             boost::type_with_alignment<boost::alignment_of<struct sockaddr_storage>::value> a_;
00130             char _b[boost::alignment_of<struct sockaddr_storage>::value];
00131         };
00132     };
00133 
00143     template <class Target>
00144     Target & sockaddr_cast(BSDSocketAddress & source);
00145 
00150     template <class Target>
00151     Target const & sockaddr_cast(BSDSocketAddress const & source);
00152 
00159     std::ostream & operator<<(std::ostream & os, BSDSocketAddress const & addr);
00160 
00172     class GenericBSDSocketAddress
00173         : public BSDSocketAddress
00174     {
00175     public:
00176         //-////////////////////////////////////////////////////////////////////////
00178         //\{
00179 
00180         GenericBSDSocketAddress();
00181         GenericBSDSocketAddress(BSDSocketAddress const & other);
00182         GenericBSDSocketAddress& operator=(const BSDSocketAddress & other);
00183 
00184         GenericBSDSocketAddress(const GenericBSDSocketAddress& other);
00185         GenericBSDSocketAddress& operator=(const GenericBSDSocketAddress& other);
00186 
00187         //\}
00188         //-////////////////////////////////////////////////////////////////////////
00190         //\{
00191 
00192         struct sockaddr const * sockaddr_p() const;
00193         struct sockaddr * sockaddr_p();
00194 
00195         using BSDSocketAddress::socklen_p;
00196 
00197         //\}
00198 
00199     protected:
00200 
00201     private:
00202         struct sockaddr_storage addr_;
00203     };
00204 
00205 }
00206 
00207 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00208 #include "BSDSocketAddress.cci"
00209 //#include "BSDSocketAddress.ct"
00210 //#include "BSDSocketAddress.cti"
00211 #endif
00212 
00213 
00214 // Local Variables:
00215 // mode: c++
00216 // fill-column: 100
00217 // comment-column: 40
00218 // c-file-style: "senf"
00219 // indent-tabs-mode: nil
00220 // ispell-local-dictionary: "american"
00221 // compile-command: "scons -u test"
00222 // End: