BSDSocketAddress.cci
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief BSDSocketAddress inline non-template implementation */
16 
17 //#include "BSDSocketAddress.ih"
18 
19 // Custom includes
20 #include <string.h>
21 #include <algorithm>
22 #include <typeinfo>
23 
24 #define prefix_ inline
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 // senf::BSDSocketAddress
29 
30 prefix_ struct sockaddr const * senf::BSDSocketAddress::sockaddr_p()
31  const
32 {
33  return static_cast<GenericBSDSocketAddress const *>(this)->sockaddr_p();
34 }
35 
36 prefix_ short senf::BSDSocketAddress::family()
37  const
38 {
39  return sockaddr_p()->sa_family;
40 }
41 
42 prefix_ socklen_t senf::BSDSocketAddress::socklen()
43  const
44 {
45  return len_;
46 }
47 
48 prefix_ socklen_t const * senf::BSDSocketAddress::socklen_p()
49  const
50 {
51  return & len_;
52 }
53 
54 prefix_ void senf::BSDSocketAddress::socklen(socklen_t len)
55 {
56  len_ = len;
57 }
58 
59 prefix_ bool senf::BSDSocketAddress::operator==(BSDSocketAddress const & other)
60  const
61 {
62  return socklen()==other.socklen() && memcmp(sockaddr_p(), other.sockaddr_p(), socklen())==0;
63 }
64 
65 prefix_ bool senf::BSDSocketAddress::operator<(BSDSocketAddress const & other)
66  const
67 {
68  if (socklen() < other.socklen()) return true;
69  else if (socklen() > other.socklen()) return false;
70  else return memcmp(sockaddr_p(), other.sockaddr_p(), socklen()) < 0;
71 }
72 
73 prefix_ bool senf::BSDSocketAddress::boolean_test()
74  const
75 {
76  return socklen() > sizeof(short) && family() != AF_UNSPEC &&
77  unsigned(std::count(reinterpret_cast<unsigned char const *>(sockaddr_p())+sizeof(short),
78  reinterpret_cast<unsigned char const *>(sockaddr_p())+socklen(),
79  0u)) < socklen()-2;
80 }
81 
82 //-/////////////////////////////////////////////////////////////////////////////////////////////////
83 // protected members
84 
85 prefix_ senf::BSDSocketAddress::BSDSocketAddress(socklen_t len, short family)
86  : len_ (len)
87 {
88  ::memset(sockaddr_p(), 0u, len_);
89  sockaddr_p()->sa_family = family;
90 }
91 
92 // WARNING: THIS COPY CONSTRUCTOR IS NOT GENERALLY SAFE !!!!!!
93 // It is only safe if:
94 // a) source and target class are identical derived classes (e.g. Both INet4)
95 // b) target is GenericBSDSocketAddress (sockaddr_storage).
96 //
97 // In these cases, the storage space available for the target is at least as large as that
98 // available for the source ant the copy is ok.
99 //
100 // To ensure this behavior, the copy constructor is protected here and is made accessible only
101 // via the corresponding derived classes.
102 //
103 // The same holds for the copy-assignment operator
104 prefix_ senf::BSDSocketAddress::BSDSocketAddress(BSDSocketAddress const & other)
105  : len_ (other.socklen())
106 {
107  ::memcpy(sockaddr_p(), other.sockaddr_p(), len_);
108 }
109 
110 prefix_ senf::BSDSocketAddress &
111 senf::BSDSocketAddress::operator=(BSDSocketAddress const & other)
112 {
113  len_ = other.socklen();
114  ::memmove(sockaddr_p(), other.sockaddr_p(), len_);
115  return *this;
116 }
117 
118 
119 prefix_ struct sockaddr * senf::BSDSocketAddress::sockaddr_p()
120 {
121  return static_cast<GenericBSDSocketAddress *>(this)->sockaddr_p();
122 }
123 
124 prefix_ socklen_t * senf::BSDSocketAddress::socklen_p()
125 {
126  return & len_;
127 }
128 
129 //-/////////////////////////////////////////////////////////////////////////////////////////////////
130 // related
131 
132 template <class Target>
133 prefix_ Target & senf::sockaddr_cast(BSDSocketAddress & source)
134 {
135  if (source.family() != Target::addressFamily)
136  throw std::bad_cast();
137  return static_cast<Target &>(source);
138 }
139 
140 template <class Target>
141 prefix_ Target const & senf::sockaddr_cast(BSDSocketAddress const & source)
142 {
143  if (source.family() != Target::addressFamily)
144  throw std::bad_cast();
145  return static_cast<Target const &>(source);
146 }
147 
148 //-/////////////////////////////////////////////////////////////////////////////////////////////////
149 // senf::GenericBSDSocketAddress
150 
151 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress()
152  : BSDSocketAddress(sizeof(sockaddr_storage), AF_UNSPEC)
153 {}
154 
155 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress(BSDSocketAddress const & other)
156  : BSDSocketAddress(other)
157 {}
158 
159 prefix_ senf::GenericBSDSocketAddress&
160 senf::GenericBSDSocketAddress::operator=(const BSDSocketAddress & other)
161 {
162  BSDSocketAddress::operator=(other);
163  return *this;
164 }
165 
166 prefix_
167 senf::GenericBSDSocketAddress::GenericBSDSocketAddress(const GenericBSDSocketAddress& other)
168  : BSDSocketAddress(other)
169 {}
170 
171 prefix_ senf::GenericBSDSocketAddress&
172 senf::GenericBSDSocketAddress::operator=(const GenericBSDSocketAddress& other)
173 {
174  BSDSocketAddress::operator=(other);
175  return *this;
176 }
177 
178 prefix_ struct sockaddr const * senf::GenericBSDSocketAddress::sockaddr_p()
179  const
180 {
181  return static_cast<struct sockaddr const *>(static_cast<void const *>(& addr_));
182 }
183 
184 prefix_ struct sockaddr * senf::GenericBSDSocketAddress::sockaddr_p()
185 {
186  return static_cast<struct sockaddr *>(static_cast<void *>(& addr_));
187 }
188 
189 //-/////////////////////////////////////////////////////////////////////////////////////////////////
190 #undef prefix_
191 
192 
193 // Local Variables:
194 // mode: c++
195 // fill-column: 100
196 // comment-column: 40
197 // c-file-style: "senf"
198 // indent-tabs-mode: nil
199 // ispell-local-dictionary: "american"
200 // compile-command: "scons -u test"
201 // End: