sockpp
Modern C++ socket library wrapper
inet6_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) 2019 Frank Pagliughi All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // 1. Redistributions of source code must retain the above copyright notice,
23 // this list of conditions and the following disclaimer.
24 //
25 // 2. Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // 3. Neither the name of the copyright holder nor the names of its
30 // contributors may be used to endorse or promote products derived from this
31 // software without specific prior written permission.
32 //
33 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
34 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
35 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
37 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 // --------------------------------------------------------------------------
45 
46 #ifndef __sockpp_inet6_addr_h
47 #define __sockpp_inet6_addr_h
48 
49 #include "sockpp/platform.h"
50 #include "sockpp/sock_address.h"
51 #include <iostream>
52 #include <string>
53 #include <cstring>
54 
55 namespace sockpp {
56 
58 
59 
65 {
67  sockaddr_in6 addr_;
68 
70  static constexpr size_t SZ = sizeof(sockaddr_in6);
71 
72 public:
74  static constexpr sa_family_t ADDRESS_FAMILY = AF_INET6;
75 
80  inet6_address() : addr_() {}
87  explicit inet6_address(in_port_t port) {
88  const in6_addr ANY IN6ADDR_ANY_INIT;
89  create(ANY, port);
90  }
98  inet6_address(const std::string& saddr, in_port_t port) {
99  create(saddr, port);
100  }
107  inet6_address(const sockaddr_storage& addr) {
108  std::memcpy(&addr_, &addr, SZ);
109  }
115  std::memcpy(&addr_, addr.sockaddr_ptr(), SZ);
116  }
121  inet6_address(const sockaddr_in6& addr) {
122  std::memcpy(&addr_, &addr, SZ);
123  }
128  inet6_address(const inet6_address& addr) : addr_(addr.addr_) {}
134  static inet6_address loopback(in_port_t port) {
135  const in6_addr LOOPBACK IN6ADDR_LOOPBACK_INIT;
136  inet6_address addr;
137  addr.create(LOOPBACK, port);
138  return addr;
139  }
146  bool is_set() const;
152  static in6_addr resolve_name(const std::string& saddr);
159  void create(const in6_addr& addr, in_port_t port);
166  void create(const std::string& saddr, in_port_t port);
172  in6_addr address() const { return addr_.sin6_addr; }
182  uint8_t operator[](int i) const {
183  return addr_.sin6_addr.s6_addr[i];
184  }
189  in_port_t port() const { return ntohs(addr_.sin6_port); }
196  socklen_t size() const override { return socklen_t(SZ); }
201  const sockaddr* sockaddr_ptr() const override {
202  return reinterpret_cast<const sockaddr*>(&addr_);
203  }
208  sockaddr* sockaddr_ptr() override {
209  return reinterpret_cast<sockaddr*>(&addr_);
210  }
216  const sockaddr_in6* sockaddr_in6_ptr() const { return &addr_; }
221  sockaddr_in6* sockaddr_in6_ptr() { return &addr_; }
230  std::string to_string() const;
231 };
232 
233 // --------------------------------------------------------------------------
234 
243 std::ostream& operator<<(std::ostream& os, const inet6_address& addr);
244 
246 // end namespace sockpp
247 }
248 
249 #endif // __sockpp_inet6_addr_h
250 
Generic socket address.
Definition: sock_address.h:64
static in6_addr resolve_name(const std::string &saddr)
Attempts to resolve the host name into a 32-bit internet address.
static inet6_address loopback(in_port_t port)
Creates an address on the loopback (localhost) interface.
Definition: inet6_address.h:134
virtual sockaddr * sockaddr_ptr()=0
Gets a pointer to this object cast to a sockaddr.
inet6_address(const std::string &saddr, in_port_t port)
Constructs an address using the name of the host and the specified port.
Definition: inet6_address.h:98
static constexpr sa_family_t ADDRESS_FAMILY
The address family for this type of address.
Definition: inet6_address.h:74
socklen_t size() const override
Gets the size of this structure.
Definition: inet6_address.h:196
bool is_set() const
Checks if the address is set to some value.
Platform-specific definitions for the sockpp library.
std::ostream & operator<<(std::ostream &os, const inet6_address &addr)
Stream inserter for the address.
in_port_t port() const
Gets the port number.
Definition: inet6_address.h:189
in6_addr address() const
Gets 128-bit IPv6 address.
Definition: inet6_address.h:172
std::string to_string() const
Gets a printable string for the address.
const sockaddr_in6 * sockaddr_in6_ptr() const
Gets a const pointer to this object cast to a sockaddr_in6.
Definition: inet6_address.h:216
inet6_address(const sockaddr_storage &addr)
Constructs the address by copying the specified structure.
Definition: inet6_address.h:107
void create(const in6_addr &addr, in_port_t port)
Creates the socket address using the specified host address and port number.
uint8_t operator[](int i) const
Gets a byte of the 128-bit IPv6 Address.
Definition: inet6_address.h:182
const sockaddr * sockaddr_ptr() const override
Gets a pointer to this object cast to a sockaddr.
Definition: inet6_address.h:201
Definition: acceptor.h:51
inet6_address(const sock_address &addr)
Constructs the address by copying the specified structure.
Definition: inet6_address.h:114
inet6_address(const inet6_address &addr)
Constructs the address by copying the specified address.
Definition: inet6_address.h:128
inet6_address(const sockaddr_in6 &addr)
Constructs the address by copying the specified structure.
Definition: inet6_address.h:121
inet6_address(in_port_t port)
Constructs an address for any iface using the specified port.
Definition: inet6_address.h:87
Class that represents an internet (IPv6) address.
Definition: inet6_address.h:64
inet6_address()
Constructs an empty address.
Definition: inet6_address.h:80
sockaddr * sockaddr_ptr() override
Gets a pointer to this object cast to a sockaddr.
Definition: inet6_address.h:208
Generic address class for sockpp.
sockaddr_in6 * sockaddr_in6_ptr()
Gets a pointer to this object cast to a sockaddr_in6.
Definition: inet6_address.h:221