sockpp
Modern C++ socket library wrapper
inet_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_inet_addr_h
48 #define __sockpp_inet_addr_h
49 
50 #include "sockpp/sock_address.h"
51 #include <iostream>
52 #include <string>
53 #include <cstring>
54 #include <algorithm>
55 
56 namespace sockpp {
57 
59 
65 class inet_address : public sock_address
66 {
68  sockaddr_in addr_;
69 
71  static constexpr size_t SZ = sizeof(sockaddr_in);
72 
73 public:
75  static constexpr sa_family_t ADDRESS_FAMILY = AF_INET;
76 
81  inet_address() : addr_() {}
88  explicit inet_address(in_port_t port) {
89  create(in_addr_t(INADDR_ANY), port);
90  }
97  inet_address(uint32_t addr, in_port_t port) { create(addr, port); }
105  inet_address(const std::string& saddr, in_port_t port) {
106  create(saddr, port);
107  }
112  inet_address(const sockaddr& addr) {
113  std::memcpy(&addr_, &addr, SZ);
114  }
119  inet_address(const sock_address& addr) {
120  std::memcpy(&addr_, addr.sockaddr_ptr(), SZ);
121  }
126  inet_address(const sockaddr_in& addr) : addr_(addr) {}
131  inet_address(const inet_address& addr) : addr_(addr.addr_) {}
138  bool is_set() const;
145  static in_addr_t resolve_name(const std::string& saddr);
152  void create(in_addr_t addr, in_port_t port);
160  void create(const std::string& saddr, in_port_t port);
165  in_addr_t address() const { return ntohl(addr_.sin_addr.s_addr); }
171  uint8_t operator[](int i) const {
172  in_addr_t addr = address();
173  return ((const uint8_t*)&addr)[i];
174  }
179  in_port_t port() const { return ntohs(addr_.sin_port); }
186  socklen_t size() const override { return socklen_t(SZ); }
191  const sockaddr* sockaddr_ptr() const override {
192  return reinterpret_cast<const sockaddr*>(&addr_);
193  }
198  sockaddr* sockaddr_ptr() override {
199  return reinterpret_cast<sockaddr*>(&addr_);
200  }
205  const sockaddr_in* sockaddr_in_ptr() const {
206  return static_cast<const sockaddr_in*>(&addr_);
207  }
212  sockaddr_in* sockaddr_in_ptr() {
213  return static_cast<sockaddr_in*>(&addr_);
214  }
222  std::string to_string() const;
223 };
224 
225 // --------------------------------------------------------------------------
226 
235 std::ostream& operator<<(std::ostream& os, const inet_address& addr);
236 
238 // end namespace sockpp
239 }
240 
241 #endif // __sockpp_inet_addr_h
242 
Generic socket address.
Definition: sock_address.h:64
static in_addr_t resolve_name(const std::string &saddr)
Attempts to resolve the host name into a 32-bit internet address.
virtual sockaddr * sockaddr_ptr()=0
Gets a pointer to this object cast to a sockaddr.
static constexpr sa_family_t ADDRESS_FAMILY
The address family for this type of address.
Definition: inet_address.h:75
inet_address(const sockaddr &addr)
Constructs the address by copying the specified structure.
Definition: inet_address.h:112
std::string to_string() const
Gets a printable string for the address.
inet_address(in_port_t port)
Constructs an address for any iface using the specified port.
Definition: inet_address.h:88
uint8_t operator[](int i) const
Gets a byte of the 32-bit Internet Address.
Definition: inet_address.h:171
sockaddr_in * sockaddr_in_ptr()
Gets a pointer to this object cast to a sockaddr_in.
Definition: inet_address.h:212
std::ostream & operator<<(std::ostream &os, const inet6_address &addr)
Stream inserter for the address.
in_addr_t address() const
Gets the 32-bit internet address.
Definition: inet_address.h:165
inet_address(const sock_address &addr)
Constructs the address by copying the specified structure.
Definition: inet_address.h:119
void create(in_addr_t addr, in_port_t port)
Creates the socket address using the specified host address and port number.
sockaddr * sockaddr_ptr() override
Gets a pointer to this object cast to a sockaddr.
Definition: inet_address.h:198
inet_address(const sockaddr_in &addr)
Constructs the address by copying the specified structure.
Definition: inet_address.h:126
inet_address(uint32_t addr, in_port_t port)
Constructs an address for the specified host using the specified port.
Definition: inet_address.h:97
Definition: acceptor.h:51
socklen_t size() const override
Gets the size of this structure.
Definition: inet_address.h:186
in_port_t port() const
Gets the port number.
Definition: inet_address.h:179
Class that represents an internet (IPv4) address.
Definition: inet_address.h:65
inet_address(const std::string &saddr, in_port_t port)
Constructs an address using the name of the host and the specified port.
Definition: inet_address.h:105
inet_address(const inet_address &addr)
Constructs the address by copying the specified address.
Definition: inet_address.h:131
bool is_set() const
Checks if the address is set to some value.
const sockaddr_in * sockaddr_in_ptr() const
Gets a const pointer to this object cast to a sockaddr_in.
Definition: inet_address.h:205
inet_address()
Constructs an empty address.
Definition: inet_address.h:81
Generic address class for sockpp.
const sockaddr * sockaddr_ptr() const override
Gets a pointer to this object cast to a sockaddr.
Definition: inet_address.h:191