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
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
15 \brief BSDSocketAddress inline non-template implementation */
17 //#include "BSDSocketAddress.ih"
24 #define prefix_ inline
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 // senf::BSDSocketAddress
30 prefix_ struct sockaddr const * senf::BSDSocketAddress::sockaddr_p()
33 return static_cast<GenericBSDSocketAddress const *>(this)->sockaddr_p();
36 prefix_ short senf::BSDSocketAddress::family()
39 return sockaddr_p()->sa_family;
42 prefix_ socklen_t senf::BSDSocketAddress::socklen()
48 prefix_ socklen_t const * senf::BSDSocketAddress::socklen_p()
54 prefix_ void senf::BSDSocketAddress::socklen(socklen_t len)
59 prefix_ bool senf::BSDSocketAddress::operator==(BSDSocketAddress const & other)
62 return socklen()==other.socklen() && memcmp(sockaddr_p(), other.sockaddr_p(), socklen())==0;
65 prefix_ bool senf::BSDSocketAddress::operator<(BSDSocketAddress const & other)
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;
73 prefix_ bool senf::BSDSocketAddress::boolean_test()
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(),
82 //-/////////////////////////////////////////////////////////////////////////////////////////////////
85 prefix_ senf::BSDSocketAddress::BSDSocketAddress(socklen_t len, short family)
88 ::memset(sockaddr_p(), 0u, len_);
89 sockaddr_p()->sa_family = family;
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).
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.
100 // To ensure this behavior, the copy constructor is protected here and is made accessible only
101 // via the corresponding derived classes.
103 // The same holds for the copy-assignment operator
104 prefix_ senf::BSDSocketAddress::BSDSocketAddress(BSDSocketAddress const & other)
105 : len_ (other.socklen())
107 ::memcpy(sockaddr_p(), other.sockaddr_p(), len_);
110 prefix_ senf::BSDSocketAddress &
111 senf::BSDSocketAddress::operator=(BSDSocketAddress const & other)
113 len_ = other.socklen();
114 ::memmove(sockaddr_p(), other.sockaddr_p(), len_);
119 prefix_ struct sockaddr * senf::BSDSocketAddress::sockaddr_p()
121 return static_cast<GenericBSDSocketAddress *>(this)->sockaddr_p();
124 prefix_ socklen_t * senf::BSDSocketAddress::socklen_p()
129 //-/////////////////////////////////////////////////////////////////////////////////////////////////
132 template <class Target>
133 prefix_ Target & senf::sockaddr_cast(BSDSocketAddress & source)
135 if (source.family() != Target::addressFamily)
136 throw std::bad_cast();
137 return static_cast<Target &>(source);
140 template <class Target>
141 prefix_ Target const & senf::sockaddr_cast(BSDSocketAddress const & source)
143 if (source.family() != Target::addressFamily)
144 throw std::bad_cast();
145 return static_cast<Target const &>(source);
148 //-/////////////////////////////////////////////////////////////////////////////////////////////////
149 // senf::GenericBSDSocketAddress
151 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress()
152 : BSDSocketAddress(sizeof(sockaddr_storage), AF_UNSPEC)
155 prefix_ senf::GenericBSDSocketAddress::GenericBSDSocketAddress(BSDSocketAddress const & other)
156 : BSDSocketAddress(other)
159 prefix_ senf::GenericBSDSocketAddress&
160 senf::GenericBSDSocketAddress::operator=(const BSDSocketAddress & other)
162 BSDSocketAddress::operator=(other);
167 senf::GenericBSDSocketAddress::GenericBSDSocketAddress(const GenericBSDSocketAddress& other)
168 : BSDSocketAddress(other)
171 prefix_ senf::GenericBSDSocketAddress&
172 senf::GenericBSDSocketAddress::operator=(const GenericBSDSocketAddress& other)
174 BSDSocketAddress::operator=(other);
178 prefix_ struct sockaddr const * senf::GenericBSDSocketAddress::sockaddr_p()
181 return static_cast<struct sockaddr const *>(static_cast<void const *>(& addr_));
184 prefix_ struct sockaddr * senf::GenericBSDSocketAddress::sockaddr_p()
186 return static_cast<struct sockaddr *>(static_cast<void *>(& addr_));
189 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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"