sockpp
Modern C++ socket library wrapper
sock_address.h
Go to the documentation of this file.
1 
13 // --------------------------------------------------------------------------
14 // This file is part of the "sockpp" C++ socket library.
15 //
16 // Copyright (c) 2014-2019 Frank Pagliughi
17 // All rights reserved.
18 //
19 // Redistribution and use in source and binary forms, with or without
20 // modification, are permitted provided that the following conditions are
21 // met:
22 //
23 // 1. Redistributions of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
25 //
26 // 2. Redistributions in binary form must reproduce the above copyright
27 // notice, this list of conditions and the following disclaimer in the
28 // documentation and/or other materials provided with the distribution.
29 //
30 // 3. Neither the name of the copyright holder nor the names of its
31 // contributors may be used to endorse or promote products derived from this
32 // software without specific prior written permission.
33 //
34 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
35 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
36 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
38 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 // --------------------------------------------------------------------------
46 
47 #ifndef __sockpp_sock_address_h
48 #define __sockpp_sock_address_h
49 
50 #include "sockpp/platform.h"
51 #include <cstring>
52 #include <stdexcept>
53 
54 namespace sockpp {
55 
57 
65 {
66 public:
70  virtual ~sock_address() {}
77  virtual socklen_t size() const =0;
82  virtual sockaddr* sockaddr_ptr() =0;
87  virtual const sockaddr* sockaddr_ptr() const =0;
93  virtual sa_family_t family() const {
94  auto p = sockaddr_ptr();
95  return p ? p->sa_family : AF_UNSPEC;
96  }
97 };
98 
100 
109 {
111  sockaddr_storage addr_;
113  socklen_t sz_;
114 
116  static constexpr size_t MAX_SZ = sizeof(sockaddr_storage);
117 
118 public:
123  sock_address_any() : addr_{}, sz_{MAX_SZ} {}
131  sock_address_any(const sockaddr* addr, socklen_t n) {
132  if (n > MAX_SZ)
133  throw std::length_error("Address length out of range");
134  std::memcpy(&addr_, addr, sz_ = n);
135  }
143  sock_address_any(const sockaddr_storage& addr, socklen_t n) {
144  if (n > MAX_SZ)
145  throw std::length_error("Address length out of range");
146  std::memcpy(&addr_, &addr, sz_ = n);
147  }
153  : sock_address_any(addr.sockaddr_ptr(), addr.size()) {}
159  socklen_t size() const override { return sz_; }
164  const sockaddr* sockaddr_ptr() const override {
165  return reinterpret_cast<const sockaddr*>(&addr_);
166  }
171  sockaddr* sockaddr_ptr() override {
172  return reinterpret_cast<sockaddr*>(&addr_);
173  }
174 };
175 
183 inline bool operator==(const sock_address& lhs, const sock_address& rhs) {
184  return lhs.size() == rhs.size() &&
185  std::memcmp(lhs.sockaddr_ptr(), rhs.sockaddr_ptr(), lhs.size()) == 0;
186 }
187 
195 inline bool operator!=(const sock_address& lhs, const sock_address& rhs) {
196  return !operator==(lhs, rhs);
197 }
198 
200 // end namespace 'sockpp'
201 }
202 
203 #endif // __sockpp_sock_address_h
Generic socket address.
Definition: sock_address.h:64
virtual sockaddr * sockaddr_ptr()=0
Gets a pointer to this object cast to a sockaddr.
virtual ~sock_address()
Virtual destructor.
Definition: sock_address.h:70
Generic socket address.
Definition: sock_address.h:108
sock_address_any()
Constructs an empty address.
Definition: sock_address.h:123
Platform-specific definitions for the sockpp library.
bool operator==(const sock_address &lhs, const sock_address &rhs)
Determines if the two objects refer to the same address.
Definition: sock_address.h:183
sockaddr * sockaddr_ptr() override
Gets a pointer to this object cast to a sockaddr.
Definition: sock_address.h:171
const sockaddr * sockaddr_ptr() const override
Gets a pointer to this object cast to a sockaddr.
Definition: sock_address.h:164
Definition: acceptor.h:51
bool operator!=(const sock_address &lhs, const sock_address &rhs)
Determines if the two objects refer to the different address.
Definition: sock_address.h:195
socklen_t size() const override
Gets the size of the address.
Definition: sock_address.h:159
virtual socklen_t size() const =0
Gets the size of this structure.
sock_address_any(const sockaddr *addr, socklen_t n)
Constructs an address.
Definition: sock_address.h:131
virtual sa_family_t family() const
Gets the network family of the address.
Definition: sock_address.h:93
sock_address_any(const sock_address &addr)
Copies another address to this one.
Definition: sock_address.h:152
sock_address_any(const sockaddr_storage &addr, socklen_t n)
Constructs an address.
Definition: sock_address.h:143