BSDSocketAddress.cci

Go to the documentation of this file.
00001 // $Id: BSDSocketAddress.cci 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 //#include "BSDSocketAddress.ih"
00027 
00028 // Custom includes
00029 #include <memory.h>
00030 #include <algorithm>
00031 #include <typeinfo>
00032 
00033 #define prefix_ inline
00034 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00035 
00036 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00037 // senf::BSDSocketAddress
00038 
00039 prefix_ struct sockaddr const * senf::BSDSocketAddress::sockaddr_p()
00040     const
00041 {
00042     return static_cast<GenericBSDSocketAddress const *>(this)->sockaddr_p();
00043 }
00044 
00045 prefix_ short senf::BSDSocketAddress::family()
00046     const
00047 {
00048     return sockaddr_p()->sa_family;
00049 }
00050 
00051 prefix_ socklen_t senf::BSDSocketAddress::socklen()
00052     const
00053 {
00054     return len_;
00055 }
00056 
00057 prefix_ socklen_t const * senf::BSDSocketAddress::socklen_p()
00058     const
00059 {
00060     return & len_;
00061 }
00062 
00063 prefix_ void senf::BSDSocketAddress::socklen(socklen_t len)
00064 {
00065     len_ = len;
00066 }
00067 
00068 prefix_ bool senf::BSDSocketAddress::operator==(BSDSocketAddress const & other)
00069     const
00070 {
00071     return socklen()==other.socklen() && memcmp(sockaddr_p(), other.sockaddr_p(), socklen())==0;
00072 }
00073 
00074 prefix_ bool senf::BSDSocketAddress::operator<(BSDSocketAddress const & other)
00075     const
00076 {
00077     if (socklen() < other.socklen()) return true;
00078     else if (socklen() > other.socklen()) return false;
00079     else return memcmp(sockaddr_p(), other.sockaddr_p(), socklen()) < 0;
00080 }
00081 
00082 prefix_ bool senf::BSDSocketAddress::boolean_test()
00083     const
00084 {
00085     return socklen() > sizeof(short) && family() != AF_UNSPEC &&
00086         unsigned(std::count(reinterpret_cast<unsigned char const *>(sockaddr_p())+sizeof(short),
00087                             reinterpret_cast<unsigned char const *>(sockaddr_p())+socklen(),
00088                    0u)) < socklen()-2;
00089 }
00090 
00091 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00092 // protected members
00093 
00094 prefix_ senf::BSDSocketAddress::BSDSocketAddress(socklen_t len, short family)
00095     : len_ (len)
00096 {
00097     ::memset(sockaddr_p(), 0u, len_);
00098     sockaddr_p()->sa_family = family;
00099 }
00100 
00101 // WARNING: THIS COPY CONSTRUCTOR IS NOT GENERALLY SAFE !!!!!!
00102 // It is only safe if:
00103 // a) source and target class are identical derived classes (e.g. Both INet4)
00104 // b) target is GenericBSDSocketAddress (sockaddr_storage).
00105 //
00106 // In these cases, the storage space available for the target is at least as large as that
00107 // available for the source ant the copy is ok.
00108 //
00109 // To ensure this behavior, the copy constructor is protected here and is made accessible only
00110 // via the corresponding derived classes.
00111 //
00112 // The same holds for the copy-assignment operator
00113 prefix_ senf::BSDSocketAddress::BSDSocketAddress(BSDSocketAddress const & other)
00114     : len_ (other.socklen())
00115 {
00116     ::memcpy(sockaddr_p(), other.sockaddr_p(), len_);
00117 }
00118 
00119 prefix_ senf::BSDSocketAddress &
00120 senf::BSDSocketAddress::operator=(BSDSocketAddress const & other)
00121 {
00122     len_ = other.socklen();
00123     ::memmove(sockaddr_p(), other.sockaddr_p(), len_);
00124     return *this;
00125 }
00126 
00127 
00128 prefix_ struct sockaddr * senf::BSDSocketAddress::sockaddr_p()
00129 {
00130     return static_cast<GenericBSDSocketAddress *>(this)->sockaddr_p();
00131 }
00132 
00133 prefix_ socklen_t * senf::BSDSocketAddress::socklen_p()
00134 {
00135     return & len_;
00136 }
00137 
00138 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00139 // related
00140 
00141 template <class Target>
00142 prefix_ Target & senf::sockaddr_cast(BSDSocketAddress & source)
00143 {
00144     if (source.family() != Target::addressFamily)
00145         throw std::bad_cast();
00146     return static_cast<Target &>(source);
00147 }
00148 
00149 template <class Target>
00150 prefix_ Target const & senf::sockaddr_cast(BSDSocketAddress const & source)
00151 {
00152     if (source.family() != Target::addressFamily)
00153         throw std::bad_cast();
00154     return static_cast<Target const &>(source);
00155 }
00156 
00157 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00158 // senf::GenericBSDSocketAddress
00159 
00160 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress()
00161     : BSDSocketAddress(sizeof(sockaddr_storage), AF_UNSPEC)
00162 {}
00163 
00164 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress(BSDSocketAddress const & other)
00165     : BSDSocketAddress(other)
00166 {}
00167 
00168 prefix_ senf::GenericBSDSocketAddress&
00169 senf::GenericBSDSocketAddress::operator=(const BSDSocketAddress & other)
00170 {
00171     BSDSocketAddress::operator=(other);
00172     return *this;
00173 }
00174 
00175 prefix_
00176 senf::GenericBSDSocketAddress::GenericBSDSocketAddress(const GenericBSDSocketAddress& other)
00177     : BSDSocketAddress(other)
00178 {}
00179 
00180 prefix_ senf::GenericBSDSocketAddress&
00181 senf::GenericBSDSocketAddress::operator=(const GenericBSDSocketAddress& other)
00182 {
00183     BSDSocketAddress::operator=(other);
00184     return *this;
00185 }
00186 
00187 prefix_ struct sockaddr const * senf::GenericBSDSocketAddress::sockaddr_p()
00188     const
00189 {
00190     return static_cast<struct sockaddr const *>(static_cast<void const *>(& addr_));
00191 }
00192 
00193 prefix_ struct sockaddr * senf::GenericBSDSocketAddress::sockaddr_p()
00194 {
00195     return static_cast<struct sockaddr *>(static_cast<void *>(& addr_));
00196 }
00197 
00198 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00199 #undef prefix_
00200 
00201 
00202 // Local Variables:
00203 // mode: c++
00204 // fill-column: 100
00205 // comment-column: 40
00206 // c-file-style: "senf"
00207 // indent-tabs-mode: nil
00208 // ispell-local-dictionary: "american"
00209 // compile-command: "scons -u test"
00210 // End: