BSDSocketAddress.hh
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 
17 #ifndef HH_SENF_Socket_Protocols_BSDSocketAddress_
18 #define HH_SENF_Socket_Protocols_BSDSocketAddress_ 1
19 
20 // Custom includes
21 #include <iosfwd>
22 #include <sys/socket.h>
23 #include <boost/type_traits/alignment_of.hpp>
24 #include <boost/type_traits/type_with_alignment.hpp>
25 #include <boost/operators.hpp>
26 #include <senf/Utils/safe_bool.hh>
27 
28 //#include "BSDSocketAddress.mpp"
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
31 namespace senf {
32 
68  : public senf::comparable_safe_bool<BSDSocketAddress>,
69  public boost::less_than_comparable<BSDSocketAddress>,
70  public boost::equality_comparable<BSDSocketAddress>
71  {
72  public:
73  bool operator==(BSDSocketAddress const & other) const;
74 
77  bool operator<(BSDSocketAddress const & other) const;
78 
82  bool boolean_test() const;
83 
88  short family() const;
89 
93  //-////////////////////////////////////////////////////////////////////////
95  //\{
96 
97  struct sockaddr const * sockaddr_p() const;
98  socklen_t socklen() const;
99  socklen_t const * socklen_p() const;
100 
101  //\}
102 
103  protected:
104  BSDSocketAddress(socklen_t len, short family);
105  BSDSocketAddress(BSDSocketAddress const & other);
107 
108  struct sockaddr * sockaddr_p();
109  socklen_t * socklen_p();
110 
111  void socklen(socklen_t len);
112 
113  private:
114  // The following incantation is needed to fix the alignment of the sockaddr data members
115  // which will be added by the derived classes later: The alignment must be forced
116  // to coincide with the struct sockaddr_storage alignment (which must have the largest
117  // alignment of all sockaddr types).
118  union {
119  socklen_t len_;
120  boost::type_with_alignment<boost::alignment_of<struct sockaddr_storage>::value> a_;
121  char _b[boost::alignment_of<struct sockaddr_storage>::value];
122  };
123  };
124 
134  template <class Target>
135  Target & sockaddr_cast(BSDSocketAddress & source);
136 
141  template <class Target>
142  Target const & sockaddr_cast(BSDSocketAddress const & source);
143 
150  std::ostream & operator<<(std::ostream & os, BSDSocketAddress const & addr);
151 
164  : public BSDSocketAddress
165  {
166  public:
167  //-////////////////////////////////////////////////////////////////////////
169  //\{
170 
174 
177 
178  //\}
179  //-////////////////////////////////////////////////////////////////////////
181  //\{
182 
183  struct sockaddr const * sockaddr_p() const;
184  struct sockaddr * sockaddr_p();
185 
187 
188  //\}
189 
190  protected:
191 
192  private:
193  struct sockaddr_storage addr_;
194  };
195 
196 }
197 
198 //-/////////////////////////////////////////////////////////////////////////////////////////////////
199 #include "BSDSocketAddress.cci"
200 //#include "BSDSocketAddress.ct"
201 //#include "BSDSocketAddress.cti"
202 #endif
203 
204 
205 // Local Variables:
206 // mode: c++
207 // fill-column: 100
208 // comment-column: 40
209 // c-file-style: "senf"
210 // indent-tabs-mode: nil
211 // ispell-local-dictionary: "american"
212 // compile-command: "scons -u test"
213 // End:
bool operator<(BSDSocketAddress const &other) const
Compare two arbitrary addresses.
boost::type_with_alignment< boost::alignment_of< struct sockaddr_storage >::value > a_
char _b[boost::alignment_of< struct sockaddr_storage >::value]
bool boolean_test() const
Return true, if address is not empty.
BSDSocketAddress & operator=(BSDSocketAddress const &other)
socklen_t const * socklen_p() const
BSDSocketAddress(socklen_t len, short family)
Socket addressing, BSD style.
struct sockaddr const * sockaddr_p() const
bool operator==(BSDSocketAddress const &other) const
Compare two arbitrary addresses.
std::ostream & operator<<(std::ostream &os, BSDSocketAddress const &addr)
Output generic socket address.
short family() const
Return the address family.
Target & sockaddr_cast(BSDSocketAddress &source)
Safe socket address down-cast.
socklen_t socklen() const
Generic BSD sockaddr storage.