Wizard
Software Engineering Project - Wizard
Loading...
Searching...
No Matches
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
56namespace sockpp {
57
59
66{
68 sockaddr_in addr_;
69
71 static constexpr size_t SZ = sizeof(sockaddr_in);
72
73public:
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 }
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
235std::ostream& operator<<(std::ostream& os, const inet_address& addr);
236
238// end namespace sockpp
239}
240
241#endif // __sockpp_inet_addr_h
242
Definition inet_address.h:66
inet_address(const inet_address &addr)
Definition inet_address.h:131
inet_address()
Definition inet_address.h:81
in_port_t port() const
Definition inet_address.h:179
inet_address(const sock_address &addr)
Definition inet_address.h:119
static in_addr_t resolve_name(const std::string &saddr)
Definition inet_address.cpp:54
socklen_t size() const override
Definition inet_address.h:186
const sockaddr_in * sockaddr_in_ptr() const
Definition inet_address.h:205
std::string to_string() const
Definition inet_address.cpp:104
inet_address(const sockaddr &addr)
Definition inet_address.h:112
bool is_set() const
Definition inet_address.cpp:46
inet_address(uint32_t addr, in_port_t port)
Definition inet_address.h:97
in_addr_t address() const
Definition inet_address.h:165
inet_address(in_port_t port)
Definition inet_address.h:88
uint8_t operator[](int i) const
Definition inet_address.h:171
const sockaddr * sockaddr_ptr() const override
Definition inet_address.h:191
void create(in_addr_t addr, in_port_t port)
inet_address(const std::string &saddr, in_port_t port)
Definition inet_address.h:105
sockaddr * sockaddr_ptr() override
Definition inet_address.h:198
sockaddr_in * sockaddr_in_ptr()
Definition inet_address.h:212
inet_address(const sockaddr_in &addr)
Definition inet_address.h:126
static constexpr sa_family_t ADDRESS_FAMILY
Definition inet_address.h:75
Definition sock_address.h:65
virtual sockaddr * sockaddr_ptr()=0