4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief BSDSocketAddress inline non-template implementation */
31 //#include "BSDSocketAddress.ih"
38 #define prefix_ inline
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42 // senf::BSDSocketAddress
44 prefix_ struct sockaddr const * senf::BSDSocketAddress::sockaddr_p()
47 return static_cast<GenericBSDSocketAddress const *>(this)->sockaddr_p();
50 prefix_ short senf::BSDSocketAddress::family()
53 return sockaddr_p()->sa_family;
56 prefix_ socklen_t senf::BSDSocketAddress::socklen()
62 prefix_ socklen_t const * senf::BSDSocketAddress::socklen_p()
68 prefix_ void senf::BSDSocketAddress::socklen(socklen_t len)
73 prefix_ bool senf::BSDSocketAddress::operator==(BSDSocketAddress const & other)
76 return socklen()==other.socklen() && memcmp(sockaddr_p(), other.sockaddr_p(), socklen())==0;
79 prefix_ bool senf::BSDSocketAddress::operator<(BSDSocketAddress const & other)
82 if (socklen() < other.socklen()) return true;
83 else if (socklen() > other.socklen()) return false;
84 else return memcmp(sockaddr_p(), other.sockaddr_p(), socklen()) < 0;
87 prefix_ bool senf::BSDSocketAddress::boolean_test()
90 return socklen() > sizeof(short) && family() != AF_UNSPEC &&
91 unsigned(std::count(reinterpret_cast<unsigned char const *>(sockaddr_p())+sizeof(short),
92 reinterpret_cast<unsigned char const *>(sockaddr_p())+socklen(),
96 //-/////////////////////////////////////////////////////////////////////////////////////////////////
99 prefix_ senf::BSDSocketAddress::BSDSocketAddress(socklen_t len, short family)
102 ::memset(sockaddr_p(), 0u, len_);
103 sockaddr_p()->sa_family = family;
106 // WARNING: THIS COPY CONSTRUCTOR IS NOT GENERALLY SAFE !!!!!!
107 // It is only safe if:
108 // a) source and target class are identical derived classes (e.g. Both INet4)
109 // b) target is GenericBSDSocketAddress (sockaddr_storage).
111 // In these cases, the storage space available for the target is at least as large as that
112 // available for the source ant the copy is ok.
114 // To ensure this behavior, the copy constructor is protected here and is made accessible only
115 // via the corresponding derived classes.
117 // The same holds for the copy-assignment operator
118 prefix_ senf::BSDSocketAddress::BSDSocketAddress(BSDSocketAddress const & other)
119 : len_ (other.socklen())
121 ::memcpy(sockaddr_p(), other.sockaddr_p(), len_);
124 prefix_ senf::BSDSocketAddress &
125 senf::BSDSocketAddress::operator=(BSDSocketAddress const & other)
127 len_ = other.socklen();
128 ::memmove(sockaddr_p(), other.sockaddr_p(), len_);
133 prefix_ struct sockaddr * senf::BSDSocketAddress::sockaddr_p()
135 return static_cast<GenericBSDSocketAddress *>(this)->sockaddr_p();
138 prefix_ socklen_t * senf::BSDSocketAddress::socklen_p()
143 //-/////////////////////////////////////////////////////////////////////////////////////////////////
146 template <class Target>
147 prefix_ Target & senf::sockaddr_cast(BSDSocketAddress & source)
149 if (source.family() != Target::addressFamily)
150 throw std::bad_cast();
151 return static_cast<Target &>(source);
154 template <class Target>
155 prefix_ Target const & senf::sockaddr_cast(BSDSocketAddress const & source)
157 if (source.family() != Target::addressFamily)
158 throw std::bad_cast();
159 return static_cast<Target const &>(source);
162 //-/////////////////////////////////////////////////////////////////////////////////////////////////
163 // senf::GenericBSDSocketAddress
165 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress()
166 : BSDSocketAddress(sizeof(sockaddr_storage), AF_UNSPEC)
169 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress(BSDSocketAddress const & other)
170 : BSDSocketAddress(other)
173 prefix_ senf::GenericBSDSocketAddress&
174 senf::GenericBSDSocketAddress::operator=(const BSDSocketAddress & other)
176 BSDSocketAddress::operator=(other);
181 senf::GenericBSDSocketAddress::GenericBSDSocketAddress(const GenericBSDSocketAddress& other)
182 : BSDSocketAddress(other)
185 prefix_ senf::GenericBSDSocketAddress&
186 senf::GenericBSDSocketAddress::operator=(const GenericBSDSocketAddress& other)
188 BSDSocketAddress::operator=(other);
192 prefix_ struct sockaddr const * senf::GenericBSDSocketAddress::sockaddr_p()
195 return static_cast<struct sockaddr const *>(static_cast<void const *>(& addr_));
198 prefix_ struct sockaddr * senf::GenericBSDSocketAddress::sockaddr_p()
200 return static_cast<struct sockaddr *>(static_cast<void *>(& addr_));
203 //-/////////////////////////////////////////////////////////////////////////////////////////////////
210 // comment-column: 40
211 // c-file-style: "senf"
212 // indent-tabs-mode: nil
213 // ispell-local-dictionary: "american"
214 // compile-command: "scons -u test"