Branch data Line data Source code
1 : : /* 2 : : ** Copyright (C) 2022 CERN 3 : : ** 4 : : ** This software is provided 'as-is', without any express or implied 5 : : ** warranty. In no event will the authors be held liable for any damages 6 : : ** arising from the use of this software. 7 : : ** 8 : : ** Permission is granted to anyone to use this software for any purpose, 9 : : ** including commercial applications, and to alter it and redistribute it 10 : : ** freely, subject to the following restrictions: 11 : : ** 12 : : ** 1. The origin of this software must not be misrepresented; you must not 13 : : ** claim that you wrote the original software. If you use this software 14 : : ** in a product, an acknowledgment in the product documentation would be 15 : : ** appreciated but is not required. 16 : : ** 2. Altered source versions must be plainly marked as such, and must not be 17 : : ** misrepresented as being the original software. 18 : : ** 3. This notice may not be removed or altered from any source distribution. 19 : : ** 20 : : ** Created on: 2022-11-29T09:21:23 21 : : ** Author: Sylvain Fargier <sylvain.fargier@cern.ch> 22 : : */ 23 : : 24 : : #ifndef CCUT_NET_HPP__ 25 : : #define CCUT_NET_HPP__ 26 : : 27 : : #include <cstdint> 28 : : #include <memory> 29 : : 30 : : #include <logger/Logger.hpp> 31 : : #include <netinet/in.h> 32 : : #include <sys/socket.h> 33 : : 34 : : #include "../Buffer.hpp" 35 : : #include "../Cow.hpp" 36 : : 37 : : namespace ccut { 38 : : namespace net { 39 : : 40 : : /** 41 : : * @brief Network Address class 42 : : * @details wraps posix sockaddr struct 43 : : */ 44 : : class Addr 45 : : { 46 : : public: 47 : : /** 48 : : * @brief Construct an invalid Address 49 : : */ 50 : 47 : Addr() = default; 51 : 102 : Addr(const Addr &) = default; 52 : : Addr(Addr &&) = default; 53 : 11 : Addr &operator=(const Addr &) = default; 54 : 33 : Addr &operator=(Addr &&) = default; 55 : : 56 : : /** 57 : : * @brief Construct an address from string 58 : : * @details string must be either ipv4 (ex: 192.168.0.1) or ipv6 (ex: ::1) 59 : : * @details constructs an invalid address on error 60 : : */ 61 : : // cppcheck-suppress noExplicitConstructor 62 : : Addr(const std::string &addr, uint16_t port = 0); 63 : : 64 : : /** 65 : : * @brief Construct an typed address 66 : : * 67 : : * @param inet network type (AF_INET6 or AF_INET) 68 : : * @param host address to parse 69 : : * @param port port 70 : : * @details constructs an invalid address on error 71 : : */ 72 : : Addr(int inet, const std::string &host, uint16_t port = 0); 73 : : 74 : : bool operator==(const Addr &other) const; 75 : 3 : inline bool operator!=(const Addr &other) const 76 : : { 77 : 3 : return !(*this == other); 78 : : } 79 : : 80 : : /** 81 : : * @brief detach the underlying shared_ptr 82 : : * @details mainly for internal use 83 : : */ 84 : : void detach(); 85 : : 86 : : /** 87 : : * @brief check if address is valid 88 : : */ 89 : 123 : inline bool isValid() const { return bool(m_addr); } 90 : 2 : inline operator bool() const { return bool(m_addr); } 91 : : 92 : : /** 93 : : * @brief get associated port 94 : : */ 95 : : uint16_t port() const; 96 : : 97 : : /** 98 : : * @brief get associated host 99 : : * 100 : : * @return std::string 101 : : */ 102 : : std::string host() const; 103 : : 104 : : /** 105 : : * @brief get underlying sockaddr struct 106 : : */ 107 : 91 : inline const struct sockaddr *addr() const { return m_addr.get(); } 108 : : 109 : : /** 110 : : * @brief get underlying sockaddr struct size 111 : : */ 112 : : size_t size() const; 113 : : 114 : : /** 115 : : * @brief return true if address is an IPv6 address 116 : : */ 117 : : bool isV6() const; 118 : : 119 : : /** 120 : : * @brief Buffer conversion constructor 121 : : * @details port is always 0 initialized 122 : : * @param buffer buffer to create socket from (must be 4 or 16 bytes long) 123 : : */ 124 : : explicit Addr(const buffer_view_t<cow_ptr<buffer_t>> &buffer); 125 : : 126 : : /** 127 : : * @brief Buffer affectation 128 : : * @details port is always 0 initialized, this construct brings a small 129 : : * optimization if Addr was already from the same family. 130 : : * 131 : : * @param buffer buffer to create socket from (must be 4 or 16 bytes long) 132 : : * @return Addr& self pointer 133 : : */ 134 : : Addr &operator=(const buffer_view_t<cow_ptr<buffer_t>> &buffer); 135 : : 136 : 14 : static inline Addr fromBuffer(const buffer_view_t<cow_ptr<buffer_t>> &buffer) 137 : : { 138 : 14 : return Addr(buffer); 139 : : } 140 : : 141 : : bool toBuffer(buffer_view_t<cow_ptr<buffer_t>> &buffer) const; 142 : : cow_ptr<buffer_t> toBuffer() const; 143 : : 144 : : protected: 145 : : std::shared_ptr<struct sockaddr> m_addr; 146 : : }; 147 : : 148 : : typedef Addr address_t; 149 : : 150 : : const logger::Logger &operator<<(const logger::Logger &logger, 151 : : const ccut::net::address_t &addr); 152 : : 153 : : } // namespace net 154 : : } // namespace ccut 155 : : 156 : : #endif