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 if (socklen() < other.socklen()) return true;
78 else if (socklen() > other.socklen()) return false;
79 else return memcmp(sockaddr_p(), other.sockaddr_p(), socklen()) < 0;
82 prefix_ bool senf::BSDSocketAddress::boolean_test()
85 return socklen() > sizeof(short) && family() != AF_UNSPEC &&
86 unsigned(std::count(reinterpret_cast<unsigned char const *>(sockaddr_p())+sizeof(short),
87 reinterpret_cast<unsigned char const *>(sockaddr_p())+socklen(),
91 ////////////////////////////////////////
94 prefix_ senf::BSDSocketAddress::BSDSocketAddress(socklen_t len, short family)
97 ::memset(sockaddr_p(), 0u, len_);
98 sockaddr_p()->sa_family = family;
101 // WARNING: THIS COPY CONSTRUCTOR IS NOT GENERALLY SAFE !!!!!!
102 // It is only safe if:
103 // a) source and target class are identical derived classes (e.g. Both INet4)
104 // b) target is GenericBSDSocketAddress (sockaddr_storage).
106 // In these cases, the storage space available for the target is at least as large as that
107 // available for the source ant the copy is ok.
109 // To ensure this behavior, the copy constructor is protected here and is made accessible only
110 // via the corresponding derived classes.
112 // The same holds for the copy-assignment operator
113 prefix_ senf::BSDSocketAddress::BSDSocketAddress(BSDSocketAddress const & other)
114 : len_ (other.socklen())
116 ::memcpy(sockaddr_p(), other.sockaddr_p(), len_);
119 prefix_ senf::BSDSocketAddress &
120 senf::BSDSocketAddress::operator=(BSDSocketAddress const & other)
122 len_ = other.socklen();
123 ::memmove(sockaddr_p(), other.sockaddr_p(), len_);
128 prefix_ struct sockaddr * senf::BSDSocketAddress::sockaddr_p()
130 return static_cast<GenericBSDSocketAddress *>(this)->sockaddr_p();
133 prefix_ socklen_t * senf::BSDSocketAddress::socklen_p()
138 ////////////////////////////////////////
141 template <class Target>
142 prefix_ Target & senf::sockaddr_cast(BSDSocketAddress & source)
144 if (source.family() != Target::addressFamily)
145 throw std::bad_cast();
146 return static_cast<Target &>(source);
149 template <class Target>
150 prefix_ Target const & senf::sockaddr_cast(BSDSocketAddress const & source)
152 if (source.family() != Target::addressFamily)
153 throw std::bad_cast();
154 return static_cast<Target const &>(source);
157 ///////////////////////////////////////////////////////////////////////////
158 // senf::GenericBSDSocketAddress
160 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress()
161 : BSDSocketAddress(sizeof(sockaddr_storage), AF_UNSPEC)
164 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress(BSDSocketAddress const & other)
165 : BSDSocketAddress(other)
168 prefix_ senf::GenericBSDSocketAddress&
169 senf::GenericBSDSocketAddress::operator=(const BSDSocketAddress & other)
171 BSDSocketAddress::operator=(other);
176 senf::GenericBSDSocketAddress::GenericBSDSocketAddress(const GenericBSDSocketAddress& other)
177 : BSDSocketAddress(other)
180 prefix_ senf::GenericBSDSocketAddress&
181 senf::GenericBSDSocketAddress::operator=(const GenericBSDSocketAddress& other)
183 BSDSocketAddress::operator=(other);
187 prefix_ struct sockaddr const * senf::GenericBSDSocketAddress::sockaddr_p()
190 return reinterpret_cast<struct sockaddr const *>(& addr_);
193 prefix_ struct sockaddr * senf::GenericBSDSocketAddress::sockaddr_p()
195 return reinterpret_cast<struct sockaddr *>(& addr_);
198 /////////////////////////////cci.e///////////////////////////////////////
205 // comment-column: 40
206 // c-file-style: "senf"
207 // indent-tabs-mode: nil
208 // ispell-local-dictionary: "american"
209 // compile-command: "scons -u test"