Socket/Protocols: Fix BSDSocketAddress self-assignment
[senf.git] / Socket / Protocols / BSDSocketAddress.cci
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief BSDSocketAddress inline non-template implementation */
25
26 //#include "BSDSocketAddress.ih"
27
28 // Custom includes
29 #include <memory.h>
30 #include <algorithm>
31 #include <typeinfo>
32
33 #define prefix_ inline
34 ///////////////////////////////cci.p///////////////////////////////////////
35
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::BSDSocketAddress
38
39 prefix_ struct sockaddr const * senf::BSDSocketAddress::sockaddr_p()
40     const
41 {
42     return static_cast<GenericBSDSocketAddress const *>(this)->sockaddr_p();
43 }
44
45 prefix_ short senf::BSDSocketAddress::family()
46     const
47 {
48     return sockaddr_p()->sa_family;
49 }
50
51 prefix_ socklen_t senf::BSDSocketAddress::socklen()
52     const
53 {
54     return len_;
55 }
56
57 prefix_ socklen_t const * senf::BSDSocketAddress::socklen_p()
58     const
59 {
60     return & len_;
61 }
62
63 prefix_ void senf::BSDSocketAddress::socklen(socklen_t len)
64 {
65     len_ = len;
66 }
67
68 prefix_ bool senf::BSDSocketAddress::operator==(BSDSocketAddress const & other)
69     const
70 {
71     return socklen()==other.socklen() && memcmp(sockaddr_p(), other.sockaddr_p(), socklen())==0;
72 }
73
74 prefix_ bool senf::BSDSocketAddress::operator!=(BSDSocketAddress const & other)
75     const
76 {
77     return ! operator==(other);
78 }
79
80 prefix_ bool senf::BSDSocketAddress::boolean_test()
81     const
82 {
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(),
86                    0u)) < socklen()-2;
87 }
88
89 ////////////////////////////////////////
90 // protected members
91
92 prefix_ senf::BSDSocketAddress::BSDSocketAddress(socklen_t len, short family)
93     : len_ (len)
94 {
95     ::memset(sockaddr_p(), 0u, len_);
96     sockaddr_p()->sa_family = family;
97 }
98
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).
103 //
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.
106 //
107 // To ensure this behavior, the copy constructor is protected here and is made accessible only
108 // via the corresponding derived classes.
109 //
110 // The same holds for the copy-assignment operator
111 prefix_ senf::BSDSocketAddress::BSDSocketAddress(BSDSocketAddress const & other)
112     : len_ (other.socklen())
113 {
114     ::memcpy(sockaddr_p(), other.sockaddr_p(), len_);
115 }
116
117 prefix_ senf::BSDSocketAddress &
118 senf::BSDSocketAddress::operator=(BSDSocketAddress const & other)
119 {
120     len_ = other.socklen();
121     ::memmove(sockaddr_p(), other.sockaddr_p(), len_);
122     return *this;
123 }
124
125
126 prefix_ struct sockaddr * senf::BSDSocketAddress::sockaddr_p()
127 {
128     return static_cast<GenericBSDSocketAddress *>(this)->sockaddr_p();
129 }
130
131 prefix_ socklen_t * senf::BSDSocketAddress::socklen_p()
132 {
133     return & len_;
134 }
135
136 ////////////////////////////////////////
137 // related
138
139 template <class Target>
140 prefix_ Target & senf::sockaddr_cast(BSDSocketAddress & source)
141 {
142     if (source.family() != Target::addressFamily)
143         throw std::bad_cast();
144     return static_cast<Target &>(source);
145 }
146
147 template <class Target>
148 prefix_ Target const & senf::sockaddr_cast(BSDSocketAddress const & source)
149 {
150     if (source.family() != Target::addressFamily)
151         throw std::bad_cast();
152     return static_cast<Target const &>(source);
153 }
154
155 ///////////////////////////////////////////////////////////////////////////
156 // senf::GenericBSDSocketAddress
157
158 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress()
159     : BSDSocketAddress(sizeof(sockaddr_storage), AF_UNSPEC)
160 {}
161
162 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress(BSDSocketAddress const & other)
163     : BSDSocketAddress(other)
164 {}
165
166 prefix_ senf::GenericBSDSocketAddress&
167 senf::GenericBSDSocketAddress::operator=(const BSDSocketAddress & other)
168 {
169     BSDSocketAddress::operator=(other);
170     return *this;
171 }
172
173 prefix_
174 senf::GenericBSDSocketAddress::GenericBSDSocketAddress(const GenericBSDSocketAddress& other)
175     : BSDSocketAddress(other)
176 {}
177
178 prefix_ senf::GenericBSDSocketAddress&
179 senf::GenericBSDSocketAddress::operator=(const GenericBSDSocketAddress& other)
180 {
181     BSDSocketAddress::operator=(other);
182     return *this;
183 }
184
185 prefix_ struct sockaddr const * senf::GenericBSDSocketAddress::sockaddr_p()
186     const
187 {
188     return reinterpret_cast<struct sockaddr const *>(& addr_);
189 }
190
191 prefix_ struct sockaddr * senf::GenericBSDSocketAddress::sockaddr_p()
192 {
193     return reinterpret_cast<struct sockaddr *>(& addr_);
194 }
195
196 /////////////////////////////cci.e///////////////////////////////////////
197 #undef prefix_
198
199 \f
200 // Local Variables:
201 // mode: c++
202 // fill-column: 100
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"
208 // End: