4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief BSDSocketAddress inline non-template implementation */
26 //#include "BSDSocketAddress.ih"
33 #define prefix_ inline
34 ///////////////////////////////cci.p///////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::BSDSocketAddress
39 prefix_ struct sockaddr const * senf::BSDSocketAddress::sockaddr_p()
42 return static_cast<GenericBSDSocketAddress const *>(this)->sockaddr_p();
45 prefix_ short senf::BSDSocketAddress::family()
48 return sockaddr_p()->sa_family;
51 prefix_ socklen_t senf::BSDSocketAddress::socklen()
57 prefix_ socklen_t const * senf::BSDSocketAddress::socklen_p()
63 prefix_ void senf::BSDSocketAddress::socklen(socklen_t len)
68 prefix_ bool senf::BSDSocketAddress::operator==(BSDSocketAddress const & other)
71 return socklen()==other.socklen() && memcmp(sockaddr_p(), other.sockaddr_p(), socklen())==0;
74 prefix_ bool senf::BSDSocketAddress::operator!=(BSDSocketAddress const & other)
77 return ! operator==(other);
80 prefix_ bool senf::BSDSocketAddress::boolean_test()
83 return socklen() > sizeof(short) && family() != AF_UNSPEC &&
84 unsigned(std::count(reinterpret_cast<unsigned char const *>(sockaddr_p())+sizeof(short),
85 reinterpret_cast<unsigned char const *>(sockaddr_p())+socklen(),
89 ////////////////////////////////////////
92 prefix_ senf::BSDSocketAddress::BSDSocketAddress(socklen_t len, short family)
95 ::memset(sockaddr_p(), 0u, len_);
96 sockaddr_p()->sa_family = family;
99 // WARNING: THIS COPY CONSTRUCTOR IS NOT GENERALLY SAFE !!!!!!
100 // It is only safe if:
101 // a) source and target class are identical derived classes (e.g. Both INet4)
102 // b) target is GenericBSDSocketAddress (sockaddr_storage).
104 // In these cases, the storage space available for the target is at least as large as that
105 // available for the source ant the copy is ok.
107 // To ensure this behavior, the copy constructor is protected here and is made accessible only
108 // via the corresponding derived classes.
110 // The same holds for the copy-assignment operator
111 prefix_ senf::BSDSocketAddress::BSDSocketAddress(BSDSocketAddress const & other)
112 : len_ (other.socklen())
114 ::memcpy(sockaddr_p(), other.sockaddr_p(), len_);
117 prefix_ senf::BSDSocketAddress &
118 senf::BSDSocketAddress::operator=(BSDSocketAddress const & other)
120 len_ = other.socklen();
121 ::memmove(sockaddr_p(), other.sockaddr_p(), len_);
126 prefix_ struct sockaddr * senf::BSDSocketAddress::sockaddr_p()
128 return static_cast<GenericBSDSocketAddress *>(this)->sockaddr_p();
131 prefix_ socklen_t * senf::BSDSocketAddress::socklen_p()
136 ////////////////////////////////////////
139 template <class Target>
140 prefix_ Target & senf::sockaddr_cast(BSDSocketAddress & source)
142 if (source.family() != Target::addressFamily)
143 throw std::bad_cast();
144 return static_cast<Target &>(source);
147 template <class Target>
148 prefix_ Target const & senf::sockaddr_cast(BSDSocketAddress const & source)
150 if (source.family() != Target::addressFamily)
151 throw std::bad_cast();
152 return static_cast<Target const &>(source);
155 ///////////////////////////////////////////////////////////////////////////
156 // senf::GenericBSDSocketAddress
158 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress()
159 : BSDSocketAddress(sizeof(sockaddr_storage), AF_UNSPEC)
162 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress(BSDSocketAddress const & other)
163 : BSDSocketAddress(other)
166 prefix_ senf::GenericBSDSocketAddress&
167 senf::GenericBSDSocketAddress::operator=(const BSDSocketAddress & other)
169 BSDSocketAddress::operator=(other);
174 senf::GenericBSDSocketAddress::GenericBSDSocketAddress(const GenericBSDSocketAddress& other)
175 : BSDSocketAddress(other)
178 prefix_ senf::GenericBSDSocketAddress&
179 senf::GenericBSDSocketAddress::operator=(const GenericBSDSocketAddress& other)
181 BSDSocketAddress::operator=(other);
185 prefix_ struct sockaddr const * senf::GenericBSDSocketAddress::sockaddr_p()
188 return reinterpret_cast<struct sockaddr const *>(& addr_);
191 prefix_ struct sockaddr * senf::GenericBSDSocketAddress::sockaddr_p()
193 return reinterpret_cast<struct sockaddr *>(& addr_);
196 /////////////////////////////cci.e///////////////////////////////////////
203 // comment-column: 40
204 // c-file-style: "senf"
205 // indent-tabs-mode: nil
206 // ispell-local-dictionary: "american"
207 // compile-command: "scons -u test"